mfo*_*ini 3 c++ boost shared-ptr c++11
昨天我安装了clang 3.1和g ++ 4.7并尝试编译我正在研究的项目.我很惊讶地看到它没有使用两个编译器进行编译.但最令我惊讶的是,问题在于boost::shared_ptr.
显然,由于该类定义了一个移动构造函数/赋值运算符,因此隐式删除了复制构造函数.所以这段代码:
#include <boost/shared_ptr.hpp>
int main() {
boost::shared_ptr<int> x;
boost::shared_ptr<int> y(x);
}
Run Code Online (Sandbox Code Playgroud)
不编译.clang回应了这个错误:
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
^
Run Code Online (Sandbox Code Playgroud)
g ++ 4.7提供了类似的错误,指的是隐式删除的构造函数.奇怪的是boost::shared_ptr,实际上明确定义了一个复制构造函数(boost/smart_ptr/shared_ptr.hpp第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
{
}
Run Code Online (Sandbox Code Playgroud)
我正在使用boost 1.48.0.2,这是相当新的.有谁知道这里发生了什么?为什么复制构造函数在实际定义时未被检测到?这是在较新版本的智能指针库中修复的吗?我在changelogs上找不到任何东西.
这是Boost中的已知错误.Boost的旧版本(1.48及更低版本)在C++ 11下无法编译,至少不是全部.就个人而言,我使用此解决方法:
#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
Run Code Online (Sandbox Code Playgroud)
当MY_LIB_COMPILING_UNDER_CXX11是,你要么设置传递到编译器或编译器的C++ 11米的标志派生它的标志.然后,在我只使用的库的其余部分my_lib::shared_ptr.这非常有效.
| 归档时间: |
|
| 查看次数: |
2745 次 |
| 最近记录: |