加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么这个代码在Clang中工作但不是G?

发布时间:2020-12-16 03:38:02 所属栏目:百科 来源:网络整理
导读:请考虑以下代码: 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++ --versionApple LLVM version 4.2 (clang-425.0.28)
请考虑以下代码:
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).

用户提供的是在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是一个聚合.

海湾合作委员会错了.

编辑:This is in fact GCC bug 52707.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读