c++ - 为什么这段代码在 Clang++ 中有效,但在 G++ 中无效?

标签 c++ c++11 g++ clang++

考虑以下代码:

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...

然而,compiling with g++ 4.8.1 through ideone gives errors :

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; 那么它 compiles fine在 g++4.8.1 中。

我的代码中是否存在一个编译器忽略但另一个编译器没有忽略的错误?

最佳答案

C++11 8.5.1 [dcl.init.aggr] p1 定义聚合类型:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

user-provided 在 8.4.2 [dcl.fct.def.default] p4 中定义:

... A special member function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration.

Foo 有两个用户声明的构造函数,这两个构造函数在它们的第一个声明中都被显式默认或删除,因此 Foo 是一个聚合。

GCC 是错误的。

编辑:This is in fact GCC bug 52707 .

关于c++ - 为什么这段代码在 Clang++ 中有效,但在 G++ 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18216313/

相关文章:

使用自己的线程运行的 C++11 packaged_task 需要一个 join() 才能获得 future

c++ - 使用 g++ 包含来自不同文件夹的头文件

cmake - 在 cmake 混合语言项目中使用 FORTRAN 链接器

c++ - 确保返回值不是指针

c++ - 一起破解线程安全 'best' 行动方案吗?

c++ - 如何实现二维 vector 数组?

c++ - 为什么构造不完整类型的 std::unique_ptr 编译?

c++ - friend 和成员二元运算符的消歧

C++ 链接器错误,未解析的外部

C++ 当我创建一个包含链表的类的对象时,为什么我的链表模板类会失败?