出错拷贝构造的boost :: shared_ptr的使用C ++ 11 [英] Error while copy constructing boost::shared_ptr using C++11

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

问题描述

昨天我装了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.

显然,因为那类定义了移动构造函数/赋值运算符,拷贝构造函数是隐式删除。所以这code:

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);
}

不能编译。铛呼应这个错误:

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的,实际上明确定义拷贝构造函数(升压/ 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
    {
    }

我使用升压1.48.0.2,这是相当新的。有谁知道什么是怎么回事?为什么它的实际定义时没有被检测到的拷贝构造函数?这是固定在智能指针库的新版本?我无法找到任何更新日志

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.

推荐答案

这是在升压一个已知的bug。升压的旧版本(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.

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

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