Default copy constructor and assignment for class with move constructor and assignment

后端 未结 2 1876
轻奢々
轻奢々 2020-12-30 15:31

Let\'s say I have this class:

class Test
{
public:
    Test();
};

AFAIK, compiler provides default copy constructor and assignment operator

相关标签:
2条回答
  • 2020-12-30 16:17

    From 12.8-7 Copying and moving class objects [class.copy]

    If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor...

    From 12.8-18

    If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor...

    According to this, since you have declared a move constructor and a move assignment operator, but no copy assignment operator, copy constructor or destructor, you do not get the implicitly generated copy constructor or assignment operator. The compiler gives you their declarations, but they are declared as deleted.

    Here is a simplified version of your example:

    struct Test
    {
    public:
        Test(){}
        Test(Test&& other){}
        Test& operator=(Test&& other) { return *this;}
    };
    
    int main()
    {
      Test t1;
      Test t2(t1);
    }
    

    And the error produced by g++ 4.8.1

    ctors.cpp:13:13: error: use of deleted function 'constexpr Test::Test(const Test&)'

    Test t2(t1);

    ctors.cpp:1:8: note: 'constexpr Test::Test(const Test&)' is implicitly declared as deleted because 'Test' declares a move constructor or move assignment operator

    struct Test

    (emphasis mine)

    0 讨论(0)
  • 2020-12-30 16:19

    According to section 12.8 Copying and moving class objects of the C++ Standard

    7 If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4).

    18 If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defined as defaulted (8.4).

    So in your case and the copy constructor and the copy assignment operator are implicitly declared by the compiler but defined as deleted.

    0 讨论(0)
提交回复
热议问题