【发布时间】:2015-04-21 21:19:59
【问题描述】:
使用gcc 4.9,我发现使用复数类型字面量生成的类型与通过常规方式创建的类型不同,即:
typeid(complex<double>(0.0,1.0)) != typeid(1.0i)
- 我在这里犯错了吗?
- 这是编译器错误还是预期的标准行为?
- 如果是预期的标准行为:背后的基本原理是什么?
添加缺少的 MCVE
#include <complex>
using std::complex;
using namespace std::literals::complex_literals;
#include <iostream>
using std::cout;
using std::endl;
#include <typeinfo>
int main(int argc, char* argv[]) {
if (typeid(complex<double>(0.0, 1.0)) == typeid(1.0i))
cout << "types are same as expected" << endl;
else
cout << "types are unexpectedly not the same" << endl;
cout << 1.0i*1.0i << endl;
cout << complex<double>(0.0, 1.0)*complex<double>(0.0, 1.0) << endl;
}
编译说明:
g++ -std=gnu++14 complex.cpp -o complex.exe
输出:
types are unexpectedly not the same
1
(-1,0)
有趣的是,文字甚至看起来都不是一个适当的虚数。 (我确定我忽略了一些东西......)
【问题讨论】:
-
你有没有机会把它放在一个实际编译的源列表中?我认为你正在使用
std::complex<double>,它不同于_Complex,它是虚数常量的gnu-extension,但是没有MCVE很难说what你'真的在做。 -
感谢您的更新。是的,它们是不同的类型。一个来自标准库,另一个来自编译器扩展。
-
Cannot reproduce the problem in C++14 @WhozCraig
i是(也是?)C++ 14 中的文字后缀。 C++11模式下,gcc大概使用this extension -
@WhozCraig clang++ 在 c++11 模式下接受程序 带有 gnu++11 扩展 可能是一个错误 o.O
-
@dyp 当我自己看到它时,我不得不加倍考虑。我的 mac OS X clang 3.5 参考也是如此,但至少会给出一个警告,让您知道它是一个 gnu 扩展。
标签: c++ c++11 complex-numbers user-defined-literals