typeid(complex<double>(0.0,1.0)) != typeid(1.0i)

 ̄綄美尐妖づ 提交于 2019-11-29 03:47:22

The behaviour of the program depends on the language standard mode of gcc:

There is a gcc extension for a built-in literal suffix i that produces C99 complex numbers. Those are distinct built-in types like _Complex double, as opposed to the "user-defined" class (template specialization) std::complex<double> used in C++.

In C++14, C++ now has a user-defined literal suffix i for complex numbers. That is, a function complex<double> operator"" i(long double) within the std::literals::complex_literals inline namespace.

Those two literal suffixes are competing:

  • In C++11 mode, only the built-in extension is possible, but it is an extension. Hence, gcc only allows it in -std=gnu++11 mode and even warns you about it. Strangely enough, clang allows it even in -std=c++11 mode.

  • In strict C++14 mode (-std=c++14 or -std=c++1y), the built-in extension must be disabled to remove ambiguity (as far as I can tell), hence both gcc and clang selecting the user-defined literal suffix.

  • In the gnu-extension-C++14 mode -std=gnu++14, gcc chooses the built-in suffix (for backwards-compatibility?), whereas clang chooses the user-defined suffix. This looks strange, and I'd suggest looking for or filing bug reports here.

Depending on which literal suffix is chosen, you either get the built-in type _Complex double or some std::complex<double>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!