考虑以下代码:
struct Foo
{
int x, y;
Foo() = default;
Foo(const Foo&) = delete;
Foo& operator=(const Foo&) = delete;
};
int main()
{
Foo f1 {1, 2};
Foo f2 = {1, 2};
}
使用 clang++ 编译不会产生错误:
$ clang++ --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix
$ clang++ -std=c++11 -stdlib=libc++ -pedantic t.cpp -o out
...builds and runs fine...
但是,通过 ideone 使用 g++ 4.8.1 编译会出现错误:
prog.cpp: In function ‘int main()’:
prog.cpp:12:17: error: no matching function for call to ‘Foo::Foo(<brace-enclosed initializer list>)’
Foo f1 {1, 2};
^
prog.cpp:12:17: note: candidate is:
prog.cpp:5:5: note: Foo::Foo()
Foo() = default;
^
prog.cpp:5:5: note: candidate expects 0 arguments, 2 provided
prog.cpp:13:19: error: could not convert ‘{1, 2}’ from ‘<brace-enclosed initializer list>’ to ‘Foo’
Foo f2 = {1, 2};
^
如果我删除Foo(const Foo&) = delete;
,那么它在 g++4.8.1 中编译得很好。
我的代码中是否存在一个编译器忽略但另一个编译器没有忽略的错误?