【问题标题】:Can I default a private constructor in the class body or not?我可以在类主体中默认一个私有构造函数吗?
【发布时间】:2011-11-05 16:47:02
【问题描述】:

GCC 4.5 不允许我这样做:

class foo {
public:
    foo() = default;

private:
    foo(foo const&) = default;
    foo& operator=(foo const&) = default;
};

它抱怨说:

错误:声明为非公共访问的“foo::foo(const foo&)”不能在类主体中默认
错误:“foo& foo::operator=(const foo&)”声明为非公共访问不能在类主体中默认

但是,GCC 4.6 允许我这样做。哪一个是正确的?

【问题讨论】:

    标签: c++ language-lawyer c++11 defaulted-functions


    【解决方案1】:

    N3291 中没有任何内容表明您不能声明 privatedefault。请注意,这是对规范的更改,在第 8.4.2 节第 2 段中;早期版本说它们必须是公开的。

    【讨论】:

    • “如果两者都被冻结,那么在水上行走和根据规范开发软件很容易。” -- E. Berard。
    猜你喜欢
    • 2019-02-25
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 2021-10-13
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多