下面的一段代碼在g ++ 4.9.2和clang ++ 3.7.0下表現不同。哪一個是正確的?標準的哪個部分與此有關?謝謝。不同類型的三元運算符
#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 ++接受它並且clang ++給出錯誤incompatible operand types ('Derived' and 'Base')
。詳情請參閱下文。
[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.
Clang可能希望你使用明確的轉換 – fileoffset 2015-04-01 01:09:21
@dyp:Chris沒有說複製初始化意味着使用複製構造函數,該複製構造函數是false:如果存在移動構造函數,則可能不需要複製構造函數。他說由於有一個拷貝構造函數,所以拷貝初始化應該成功,這是真的,要麼有一個可用的移動構造函數,要麼沒有拷貝構造函數被使用。 – 2015-04-01 01:32:23
@BenVoigt,沒關係,當它說複製初始化時,我真的沒有足夠的注意力。進一步看,它應該歸結爲哪個構造函數是通過重載分辨率挑選出來的?Base b = Derived();' – chris 2015-04-01 01:37:55