【发布时间】:2017-04-07 03:47:46
【问题描述】:
在我的计算中,我使用虚数单位,我认为编译器应该能够在编译时通过减少类似的东西来简化这些操作
a + i b --> std::complex(a,b)
当然,以上是简化的,我的表达通常看起来更复杂(双关语)。
在 C++14 中,我可以使用复杂的文字,而在 C++11 中,我使用的是 constexpr std::complex 变量。但是,这两种方法都因英特尔 C++ 编译器 icpc 而失败,错误消息在源代码中显示为 cmets。
如何解决这些故障?
#include <complex>
auto test1(double a, double b)
{
// error #1909: complex integral types are not supported
using namespace std::complex_literals;
auto z = a + 1i*b;
return z;
}
auto test2(double a, double b)
{
// error: calling the default constructor for "std::complex<double>" does not produce a constant value
constexpr std::complex<double> I(0,1);
auto z = a + I*b;
return z;
}
auto test3(double a, double b)
{
// Can this be optimized as good as the others?
std::complex<double> I(0,1);
auto z = a + I*b;
return z;
}
额外问题: 为什么 test2 被优化掉并被跳转到 test3 所取代? (见https://godbolt.org/g/pW1JZ8)
【问题讨论】:
-
test2优化仅仅是因为允许编译器发出这样的代码;它遵循as-if规则
标签: c++ c++14 complex-numbers icc