why default copy-ctor is generated for a class with reference member variable?

后端 未结 3 1359
予麋鹿
予麋鹿 2021-01-05 02:41

http://coliru.stacked-crooked.com/a/8356f09dff0c9308

#include 
struct A
{
  A(int& var) : r(var) {}
  int &r;
};

int main(int argc,          


        
3条回答
  •  鱼传尺愫
    2021-01-05 03:36

    Copy assignment operator and Copy constructor are TWO different things. It does NOT guarantee that when copy assignment operator exist, copy constructor also exist, but they follow their own rules.

    From copy assignment operator, it mentions :
    If T has a non-static data member of a reference type, then the defaulted copy assignment operator for class T is defined as 'Deleted'. That;s the case :D

    For the Copy constructor, it has its own rule to decide whether deleted or not, for example, if Non static rvalue reference data member exist in a class, then the default copy constructor will be defined as 'Deleted'. You can see more by visit docs.

提交回复
热议问题