Bug 49476 - Complex functions returning references (not following C++ standard)
Summary: Complex functions returning references (not following C++ standard)
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.5.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-20 17:21 UTC by Basil
Modified: 2011-06-20 18:21 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Reproducer program (334 bytes, text/x-c++src)
2011-06-20 17:22 UTC, Basil
Details
Patch to fix issue (350 bytes, patch)
2011-06-20 17:22 UTC, Basil
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Basil 2011-06-20 17:21:28 UTC
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)
Comment 1 Basil 2011-06-20 17:22:12 UTC
Created attachment 24567 [details]
Reproducer program
Comment 2 Basil 2011-06-20 17:22:36 UTC
Created attachment 24568 [details]
Patch to fix issue
Comment 5 Paolo Carlini 2011-06-20 18:21:01 UTC
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.