昨天我安装了 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);
}
不编译。 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
^
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
{
}
我正在使用 boost 1.48.0.2,这是相当新的。有谁知道这里发生了什么?为什么复制构造函数在实际定义时没有被检测到?新版本的智能指针库是否修复了此问题?我在变更日志上找不到任何内容。