Using gcc 4.9
I found that types generated with type literal for complex numbers are not the same as when created by conventional means, i.e.:
typeid(complex<double>(0.0,1.0)) != typeid(1.0i)
- Am I making a mistake here?
- Is this a compiler bug or intended standard behavior?
- If intended standard behavior: What is the rationale behind?
Adding the missing 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;
}
Compile instructions:
g++ -std=gnu++14 complex.cpp -o complex.exe
Output:
types are unexpectedly not the same
1
(-1,0)
Interestingly the literal does not even seem to be a proper imaginary number. (I am sure I am overlooking something...)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…