【发布时间】:2012-07-02 23:54:04
【问题描述】:
昨天我安装了 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 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,它是相当新的。有谁知道这里发生了什么?为什么复制构造函数在实际定义时没有被检测到?这在较新版本的智能指针库中是否已修复?我在更新日志上找不到任何内容。
【问题讨论】:
-
Boost 1.48 不是新的,它已有 8 个月的历史(1.50 是当前版本),并且自从它发布以来,许多 与 C++11 相关的错误修复已经完成。我会说第 1 步是验证这在较新版本的 Boost 中是否有效(或者只使用
std::shared_ptr代替)。 -
@Gigi 同样的问题。 ildjarn 是的,8 个月大对我来说还算新鲜。问题是我不明白为什么它不起作用,因为实际上定义了复制构造函数。无论如何,我可能不得不更新我的 boost 库。
-
@mfontanini : 8 个月大在 Boost 方面已经相当老了,尤其是在前沿编译器支持方面。 :-]
-
好吧,我已经更新了 boost,它现在可以工作了:D
-
@mfontanini - 您显示的构造函数不是复制构造函数,因为它是一个模板。它是其他类型的转换构造函数。
标签: c++ boost c++11 shared-ptr