使用C ++ 11复制构造boost :: shared_ptr时出错 [英] Error while copy constructing boost::shared_ptr using C++11

查看:157
本文介绍了使用C ++ 11复制构造boost :: shared_ptr时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我安装了clang 3.1和g ++ 4.7,并尝试编译一个我正在编写的项目。我很惊讶看到它没有使用两个编译器编译。但是令我吃惊的是,问题出现在 boost :: shared_ptr 中。

Yesterday I installed clang 3.1 and g++ 4.7 and tried compiling a project I'm working on. I was surprised to see that it didn't compile using both of the compilers. But what surprises me most is that the problem is in boost::shared_ptr.

显然,由于该类定义了一个移动构造函数/赋值运算符,因此复制构造函数被隐式删除。所以这段代码:

Apparently, since that class defines a move constructor/assignment operator, the copy constructor is implicitly deleted. So this code:

#include <boost/shared_ptr.hpp>

int main() {
    boost::shared_ptr<int> x;
    boost::shared_ptr<int> y(x);
}

不编译。 clang回应此错误:

Does not compile. clang echoes this error:

test.cpp:5:28: error: call to implicitly-deleted copy constructor of
      'boost::shared_ptr<int>'
    boost::shared_ptr<int> y(x);
                           ^ ~
/usr/include/boost/smart_ptr/shared_ptr.hpp:347:5: note: copy constructor is
      implicitly deleted because 'shared_ptr<int>' has a user-declared move
      constructor
    shared_ptr( shared_ptr && r ): px( r.px ), pn() // never throws
    ^

g ++ 4.7提供了一个类似的错误,指的是隐式删除的构造函数。奇怪的是, boost :: shared_ptr 实际上显式定义了一个复制构造函数(boost / smart_ptr / shared_ptr.hpp第228行):

g++ 4.7 provides a similar error, referring to the implicitly deleted constructor as well. The weird thing is that boost::shared_ptr, actually explicitly defines a copy constructor(boost/smart_ptr/shared_ptr.hpp line 228):

    template<class Y>
#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )

    shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )

#else

    shared_ptr( shared_ptr<Y> const & r )

#endif
    : px( r.px ), pn( r.pn ) // never throws
    {
    }

我使用boost 1.48.0.2,这是相当新的。有人知道这里发生了什么吗?为什么在实际定义时,未检测到复制构造函数?这是在较新版本的智能指针库中固定的吗?我在changelogs上找不到任何东西。

I'm using boost 1.48.0.2, which is fairly new. Does anyone know what is going on in here? Why is the copy constructor not being detected when it's actually defined? Is this fixed in newer versions of the smart pointer library? I couldn't find anything on changelogs.

推荐答案

这是Boost中的一个已知错误。 Boost的旧版本(1.48和更低版本)在C ++ 11下是不可编译的,至少不是全部。就个人而言,我使用此解决方法:

This is a known bug in Boost. The older versions (1.48 and lower) of Boost are not compilable under C++11, at least, not all. Personally, I use this workaround:

#ifdef MY_LIB_COMPILING_UNDER_CXX11

#include <memory>

namespace my_lib {

using std::shared_ptr;
using std::weak_ptr;

};

#else

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

namespace my_lib {

using boost::shared_ptr;
using boost::weak_ptr;

};

#endif

其中 MY_LIB_COMPILING_UNDER_CXX11 将是一个标志,你设置传递给编译器或从编译器的C ++ 11标志导出。然后,在库的其余部分,我只使用 my_lib :: shared_ptr 。这工作得很好。

Where MY_LIB_COMPILING_UNDER_CXX11 would be a flag that you set either passed to the compiler or deriving it from the compiler's C++11 flags. And, then, in the rest of the library I only use my_lib::shared_ptr. This works very well.

这篇关于使用C ++ 11复制构造boost :: shared_ptr时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆