What's the point in defaulting functions in C++11?

后端 未结 8 1465
失恋的感觉
失恋的感觉 2020-12-03 03:41

C++11 adds the ability for telling the compiler to create a default implementation of any of the special member functions. While I can see the value of deleting a function,

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 03:44

    As well as changing the accessibility (private/protected) of generated functions, you will be able to make them virtual.

    struct S
    {
        virtual ~S();
        virtual S& operator=(const S&);
    };
    
    S::~S() = default;
    S& S::operator=(const S&) = default;
    

    The following aspects of defaulted functions can be modified:

    • access (be made non-public)
    • virtual
    • explicit (constructors)
    • exception specifications
    • const-ness of parameters

    but to do so, the functions must be defined outside the class (8.4.2/2 in the C++0x Final Committee Draft).

    A version of the original proposal by Lawrence Crowl is here.

    Thanks to Roger Pate for the clarification and citation.

提交回复
热议问题