简体   繁体   中英

Can I default a private constructor in the class body or not?

GCC 4.5 doesn't let me do this:

class foo {
public:
    foo() = default;

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

It complains that:

error: 'foo::foo(const foo&)' declared with non-public access cannot be defaulted in the class body
error: 'foo& foo::operator=(const foo&)' declared with non-public access cannot be defaulted in the class body

However, GCC 4.6 lets me do it. Which one is correct?

There is nothing in N3291 that says you cannot declare something private and default . Note that this was a change to the specification, in section 8.4.2, paragraph 2; earlier versions said that they must be public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM