简体   繁体   English

如何使用 std::shared_ptr 构建结构的 std::optional

[英]How to construct a std::optional of a struct with std::shared_ptr

I'm trying to create an optional with a class that has a shared_ptr but the implicitly deleted constructor prevents me from using make_optional to create it.我正在尝试使用具有shared_ptr的 class 创建一个可选的,但隐式删除的构造函数阻止我使用make_optional来创建它。

Is the only option to manually write that constructor?手动编写该构造函数是唯一的选择吗?

Here is a simple reproduction:这是一个简单的复制:

https://onlinegdb.com/DcWzF1NAt https://onlinegdb.com/DcWzF1NAt

#include <optional>
#include <memory>
class A
{
public:
 A(int v):
  val{v}
  {}
 int val;   
};

class B
{
public:
 B(std::shared_ptr<A>& a):
   a{a}
 {}
 
 const std::shared_ptr<A> a;   
};


int main()
{
  std::shared_ptr<A> a = std::make_shared<A>(3);
  
  std::optional<B> b;
  b = std::make_optional<B>(a);
}

causes this compile error导致这个编译错误

main.cpp:31:30: error: use of deleted function ‘std::optional& std::optional::operator=(std::optional&&)’
   b = std::make_optional<B>(a);
                              ^
In file included from main.cpp:4:0:
/usr/include/c++/7/optional:453:11: note: ‘std::optional& std::optional::operator=(std::optional&&)’ is implicitly deleted because the default definition would be ill-formed:
     class optional
           ^~~~~~~~
/usr/include/c++/7/optional:453:11: error: use of deleted function ‘std::_Enable_copy_move& std::_Enable_copy_move::operator=(std::_Enable_copy_move&&) [with _Tag = std::optional]’
In file included from /usr/include/c++/7/optional:43:0,
                 from main.cpp:4:
/usr/include/c++/7/bits/enable_special_members.h:246:5: note: declared here
     operator=(_Enable_copy_move&&) noexcept                         = delete;
     ^~~~~~~~

It's not an implicitly deleted constructor causing a problem, it's the implicitly deleted copy assignment operator and has nothing to do with std::shared_ptr , it's because you've declared the B::a member to be const .它不是导致问题的隐式删除构造函数,它是隐式删除的复制赋值运算符,与std::shared_ptr无关,这是因为您已将B::a成员声明为const The solution here is to not mark it as const :这里的解决方案是不将其标记为const

class B
{
public:
 B(std::shared_ptr<A>& a):
   a{a}
 {}

 std::shared_ptr<A> a;   
};

The reason the compiler deletes the copy assignment operator is because it has to be able to modify that member;编译器删除复制赋值运算符的原因是因为它必须能够修改该成员; we can see this with a slightly simpler example:我们可以通过一个稍微简单一点的例子来了解这一点:

class NoCopyAssign {
 public:
  NoCopyAssign(int a) : mA(a) {}
 private:
  const int mA;
 };

Ordinarily, the copy assignment operator would be roughly equivalent to this:通常,复制赋值运算符大致等同于:

NoCopyAssign &operator=(const NoCopyAssign &other) {
  mA = other.mA;
  return *this;
}

But this is impossible because mA is a const int , so you can't change its value.但这是不可能的,因为mA是一个const int ,所以你不能改变它的值。 }; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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