In the C++ standard definition, section 26.2.7, std::real() and std::imag() are defined to be: template <class T> T real (const complex <T>& x) template <class T> T imag (const complex <T>& x) GCC version 4.5.2, these functions return references, unless -std=c++0x is specified. If a specialization of std::complex returns values for real() and imag(), an error during compilation will occur. When compiling the attached reproducer (complex.cpp), the following error occurs: $ g++ complex.cpp -o complex -Wall -Wextra In file included from complex.cpp:2:0: /usr/include/c++/4.5/complex: In function ‘_Tp& std::real(std::complex<_Tp>&) [with _Tp = f32]’: complex.cpp:42:22: instantiated from here /usr/include/c++/4.5/complex:544:23: error: invalid initialization of non-const reference of type ‘f32&’ from an rvalue of type ‘value_type’ /usr/include/c++/4.5/complex: In function ‘_Tp& std::imag(std::complex<_Tp>&) [with _Tp = f32]’: complex.cpp:43:22: instantiated from here /usr/include/c++/4.5/complex:554:23: error: invalid initialization of non-const reference of type ‘f32&’ from an rvalue of type ‘value_type’ Fix: A patch has been attached to this report (along with an example that would trigger the error) Additional System Information: $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/lto-wrapper Target: i686-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.5.2-8ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.5 --enable-shared --enable-multiarch --with-multiarch-defaults=i386-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/i386-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/i386-linux-gnu --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-gold --enable-ld=default --with-plugin-ld=ld.gold --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu Thread model: posix gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
Created attachment 24567 [details] Reproducer program
Created attachment 24568 [details] Patch to fix issue
Changed by http://gcc.gnu.org/ml/libstdc++/2004-01/msg00091.html c.f. http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#387 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1589.html http://gcc.gnu.org/ml/libstdc++/2008-05/msg00120.html
The change doesn't fix the issue, as it is a solution for when -std=c++0x is specified. (In reply to comment #3) > Changed by http://gcc.gnu.org/ml/libstdc++/2004-01/msg00091.html > c.f. > http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#387 > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1589.html > http://gcc.gnu.org/ml/libstdc++/2008-05/msg00120.html
I don't think we should fix now, after 7 years, the return type of std::real and std::imag in C++03 mode, because this would cause serious ABI problems when objects build with pre- and post- <complex> header are linked together. Indeed, that's exactly why I changed the return-type only in C++0x mode, where we have leeway.