c – typeid(complex(0.0,1.0))!= typeid(1.0i)
使用
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) 有趣的是,文字似乎不是一个正确的虚构数字. (我相信我可以忽略某些东西…) 解决方法
程序的行为取决于gcc的语言标准模式:
有一个gcc extension for a built-in literal suffix 在C 14中,C现在有一个用户定义的文字后缀i用于复数.也就是说,功能复合体< double>运算符“”i(long double)在std :: literals :: complex_literals内联命名空间中. 这两个字面的后缀是竞争的: >在C 11模式下,只有内置的扩展是可能的,但它是一个扩展.因此,gcc只允许在-std = gnu 11模式下,甚至警告你.奇怪的是,clang甚至允许它在-std = c 11模式. 取决于选择哪个文字后缀,您可以获得内置类型_Complex double或一些std :: complex&double. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |