C++ 孙子女';s复制构造函数导致父级';s std::可选成员';要删除的默认赋值运算符

C++ 孙子女';s复制构造函数导致父级';s std::可选成员';要删除的默认赋值运算符,c++,C++,我不明白为什么在下面的代码中,复制构造函数会导致删除std::optional的复制赋值运算符?如果您注释掉复制构造函数,它可以编译 我需要孙子的副本构造函数,我想知道成员是否仍然是可选的 clang++ -std=c++17 test.cpp #包括 #包括 班孙{ 公众: 孙子(){} 孙子(孙子和其他){};//注释掉这一行,它就工作了??? }; 班童 { 公众: std:可选儿童; 显式子(){} 儿童(儿童和其他){}; }; 班级家长 { 公众: std:可选儿童; 父(){}

我不明白为什么在下面的代码中,复制构造函数会导致删除std::optional的复制赋值运算符?如果您注释掉复制构造函数,它可以编译

我需要
孙子的副本构造函数
,我想知道成员是否仍然是可选的

clang++ -std=c++17 test.cpp
#包括
#包括
班孙{
公众:
孙子(){}
孙子(孙子和其他){};//注释掉这一行,它就工作了???
};
班童
{
公众:
std:可选儿童;
显式子(){}
儿童(儿童和其他){};
};
班级家长
{
公众:
std:可选儿童;
父(){}
};
int main()
{
父母;
儿童;
parent.child=child;//无法分配类型为“std::\u 1::optional”的对象,因为其复制分配运算符已被隐式删除
}
test.cpp:34:18:错误:无法分配“std::\u 1::optional”类型的对象
因为它的复制赋值运算符被隐式删除
parent.child=child;//“std::\u 1::optional”类型的对象无法。。。
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/可选:757:41:注意:
此处隐式删除了显式默认函数
_LIBCPP_INLINE_可见性可选&运算符=(常量可选&)=默认值;
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/可选:583:7:注意:复制
“optional”的赋值运算符被隐式删除,因为基类
'可选的''sfinae''assign''u base''t'(又名
“std::u 1::u sfinae\u assign\u base”)具有已删除的副本分配
操作人员
,private\uuu可选\u sfinae\u assign\u base\t
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/\u元组:533:25:注意:
“operator=”已在此处显式标记为已删除
__sfinae\u assign\u base&operator=(sfinae\u assign\u base const&)=delete;

尝试将
const
添加到副本构造函数eah的参数中,这实际上解决了问题。尝试将
const
添加到副本构造函数eah的参数中,这实际上解决了问题。
#include <optional>
#include <string>

class GrandChild {
public:
    GrandChild() { }

    GrandChild(GrandChild& other) { }; // Comment out this line and it works???
};

class Child
{
public:
    std::optional<GrandChild> child;

    explicit Child() { }

    Child(Child& other) {};
};

class Parent
{
public:
    std::optional<Child> child;

    Parent() {}
};

int main() 
{
    Parent parent;
    Child child;
    parent.child = child; // object of type 'std::__1::optional<Child>' cannot be assigned because its copy assignment operator is implicitly deleted
}
test.cpp:34:18: error: object of type 'std::__1::optional<Child>' cannot be assigned
      because its copy assignment operator is implicitly deleted
    parent.child = child; // object of type 'std::__1::optional<Child>' cannot ...
                 ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/optional:757:41: note:
      explicitly defaulted function was implicitly deleted here
    _LIBCPP_INLINE_VISIBILITY optional& operator=(const optional&) = default;
                                        ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/optional:583:7: note: copy
      assignment operator of 'optional<Child>' is implicitly deleted because base class
      '__optional_sfinae_assign_base_t<Child>' (aka
      'std::__1::__sfinae_assign_base<false, false>') has a deleted copy assignment
      operator
    , private __optional_sfinae_assign_base_t<_Tp>
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__tuple:533:25: note:
      'operator=' has been explicitly marked deleted here
  __sfinae_assign_base& operator=(__sfinae_assign_base const&) = delete;