昨天我安裝了clang 3.1和g ++ 4.7,並嘗試編譯我正在編寫的項目。我驚訝地發現它並沒有使用兩種編譯器進行編譯。但最令我驚訝的是,問題出在boost::shared_ptr
。使用C++複製構建boost :: shared_ptr時出錯Error:
顯然,由於該類定義了移動構造函數/賦值運算符,所以複製構造函數被隱式刪除。所以這段代碼:
#include <boost/shared_ptr.hpp>
int main() {
boost::shared_ptr<int> x;
boost::shared_ptr<int> y(x);
}
不編譯。鏗鏘迴響這個錯誤:
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):
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,這是相當新的。有人知道這裏發生了什麼嗎?爲什麼複製構造函數在實際定義時未被檢測到?這是否固定在智能指針庫的更新版本中?我在變更日誌上找不到任何東西。
Boost 1.48並不是新的,它已經8個月了(現在是1.50),並且自從發佈以來,已經對_many_ C++ 11進行了相關的錯誤修正。我會說第1步是驗證這是否適用於更新版本的Boost(或者使用'std :: shared_ptr'代替)。 – ildjarn
@Gigi同樣的問題。 ildjarn是的,8個月大的時間對我來說相當新鮮。事情是我不明白爲什麼它不工作,因爲複製構造函數是實際定義的。無論如何,我可能會必須更新我的boost庫。 – mfontanini
@mfontanini:8個月的時間在Boost術語中非常陳舊,特別是在出色的編譯器支持方面。 : - ] – ildjarn