问题描述
我想使用clst ++的std :: shared_ptr(clang版本3.1(trunk 143100))使用libstdc ++(4.6.1)。我有一个小演示程序:
I'm trying to use the std::shared_ptr in clang++(clang version 3.1 (trunk 143100)) using libstdc++(4.6.1). I have a little demo program:
#include <memory>
int main()
{
std::shared_ptr<int> some(new int);
std::shared_ptr<int> other(some);
return 0;
}
可以使用以下命令生成:
which can be build using:
clang++ -std=c++0x -o main main.cpp
并给出以下错误输出:
main.cpp:6:23: error: call to deleted constructor of 'std::shared_ptr<int>'
std::shared_ptr<int> other(some);
^ ~~~~
/usr/include/c++/4.6/bits/shared_ptr.h:93:11: note: function has been explicitly marked
deleted here
class shared_ptr : public __shared_ptr<_Tp>
由于某种原因,它需要被删除的构造函数,因为提供了一个移动构造函数)。
但是为什么它工作编译(g ++(Ubuntu / Linaro 4.6.1-9ubuntu3)4.6.1。)?
For some reason it needs the constructor which is deleted because a move constructor is provided (which is correct behaviour).But why does it work compile with (g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1.)? Somebody any ideas on how to fix this?
推荐答案
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的工作文件中作为处理。 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.
这篇关于使用std :: shared_ptr与clang ++和libstdc ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!