使用C++时的clang ++错误消息0x:调用已删除的构造函数

pla*_*hos 5 c++ clang c++11

您好我已将我的Xcode升级到版本4.2并将++升级到版本:

Apple clang version 3.0 (tags/Apple/clang-211.10.1) (based on LLVM 3.0svn) 
Target: x86_64-apple-darwin11.2.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)

当尝试使用clang -std = c ++ 0x编译以下代码时

#include <memory>
#include <limits>
#include <boost/shared_ptr.hpp>


class ilpConstraintImpl {
public:
    virtual ~ilpConstraintImpl() {}
};


class ilpConstraint {
public:
    ilpConstraint(ilpConstraintImpl* implptr):impl(implptr) { }
public:
    boost::shared_ptr<ilpConstraintImpl> impl;
};

class ilpExprImpl {
public:
    virtual ilpConstraint operator<= (const double rs)=0;
    virtual ~ilpExprImpl() {}
};



class ilpExpr {
 public:
    virtual ~ilpExpr() {};
    ilpConstraint operator<= (const double rs) { return impl->operator<=(rs); }
    ilpExpr(ilpExprImpl* implptr):impl(implptr) { }
    boost::shared_ptr<ilpExprImpl> impl;
};
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

./test.h:46:54: error: call to deleted constructor of 'ilpConstraint'
    ilpConstraint operator<= (const double rs) { return impl->operator<=(rs); }
                                                        ^~~~~~~~~~~~~~~~~~~~
./test.h:28:7: note: function has been explicitly marked deleted here
class ilpConstraint {
      ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

不使用-std = c ++ 0x进行编译.

How*_*ant 6

这看起来像是一个铿锵的错误.我正在使用后来没有这种行为的clang版本.您可以尝试ilpConstraint将显式复制构造函数作为临时解决方法.

  • 是的,问题是较旧的shared_ptr版本声明了移动构造函数,这将使clang隐式声明并删除其复制构造函数(如C++ 11要求).在这种情况下,早期的C++ 11草案根本没有隐式声明复制构造函数(因此会使用模板).这个shared_ptr更改可能会影响事情.也许铛不会隐申报ilpConstraint此举构造函数无论出于何种原因,这意味着它只会为ilpConstraint提供缺失的拷贝构造函数(因为它不能调用的shared_ptr的拷贝构造函数). (2认同)