且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用std :: shared_ptr与clang ++和libstdc ++

更新时间:2023-11-10 17:40:34

shared_ptr的隐式声明的拷贝构造函数被删除,因为shared_ptr有一个move构造函数或者一个移动赋值运算符(或两者),每个C ++ 11 12.8p7:

The implicitly-declared copy constructor for shared_ptr is deleted because shared_ptr has a move constructor or a move assignment operator (or both), per C++11 12.8p7:

如果类定义没有显式声明一个拷贝构造函数,如果类定义声明了move构造函数或move赋值运算符,则隐式声明的拷贝构造函数定义为deleted;

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).

GCC 4.6.x没有实现这个规则,它在C ++ 11的工作文件中作为 N3203 = 10-0193 处理。 libstdc ++ 4.6.x中的shared_ptr在写入时是正确的,但是C ++ 11之后改变了.. Boost有完全相同的问题和它的shared_ptr,这是常见的不兼容性 GCC和Clang之间。

GCC 4.6.x does not implement this rule, which came into the C++11 working paper very late in the process as N3203=10-0193. The shared_ptr in libstdc++ 4.6.x was correct at the time it was written, but C++11 changed after that.. Boost had exactly the same issue with it's shared_ptr, and this is one of the common incompatibilities between GCC and Clang.

向shared_ptr添加默认的复制构造函数和复制赋值操作符将解决问题。

Adding a defaulted copy constructor and copy assignment operator to shared_ptr will fix the problem.