The following piece of code behaves differently under g++ 4.9.2 and clang++ 3.7.0. Which one is correct? What part in standard is related to this? Thanks.
#include <iostream> using namespace std; struct Base { Base() = default; Base(const Base&) = default; Base(Base&&) = delete; }; struct Derived : Base { }; int main() { const Base& b = true ? Derived() : Base(); } g++ accepts it and clang++ gives an error incompatible operand types ('Derived' and 'Base'). See below for details.
[hidden]$ g++ -v Using built-in specs. COLLECT_GCC=/usr/bin/g++ COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.9.2/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) [hidden]$ g++ -std=c++11 b.cpp [hidden]$ clang++ -v clang version 3.7.0 (http://llvm.org/git/clang.git 6bbdbba8ec8a7730c68fee94363547dc2dc65b10) Target: x86_64-unknown-linux-gnu Thread model: posix Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/3.4.6 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.9.2 Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.9.2 Candidate multilib: .;@m64 Candidate multilib: 32;@m32 Selected multilib: .;@m64 [hidden]$ clang++ -std=c++11 b.cpp b.cpp:14:24: error: incompatible operand types ('Derived' and 'Base') const Base& b = true ? Derived() : Base(); ^ ~~~~~~~~~ ~~~~~~ 1 error generated.
Base b = Derived();Base()orDerived()depending on the condition, and extend the temporary's lifetime?