2012-07-02 25 views
3

昨天我安裝了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,這是相當新的。有人知道這裏發生了什麼嗎?爲什麼複製構造函數在實際定義時未被檢測到?這是否固定在智能指針庫的更新版本中?我在變更日誌上找不到任何東西。

+5

Boost 1.48並不是新的,它已經8個月了(現在是1.50),並且自從發佈以來,已經對_many_ C++ 11進行了相關的錯誤修正。我會說第1步是驗證這是否適用於更新版本的Boost(或者使用'std :: shared_ptr'代替)。 – ildjarn

+0

@Gigi同樣的問題。 ildjarn是的,8個月大的時間對我來說相當新鮮。事情是我不明白爲什麼它不工作,因爲複製構造函數是實際定義的。無論如何,我可能會必須更新我的boost庫。 – mfontanini

+1

@mfontanini:8個月的時間在Boost術語中非常陳舊,特別是在出色的編譯器支持方面。 : - ] – ildjarn

回答

5

這是Boost中的一個已知錯誤。舊版本(1.48或更低)的Boost在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 

MY_LIB_COMPILING_UNDER_CXX11將是您設置任傳遞到編譯器或編譯器的C++ 11米的標誌派生它的標誌。然後,在圖書館的其餘部分,我只使用my_lib::shared_ptr。這工作得很好。

+1

問題是我沒有使用'boost :: shared_ptr',而是'boost :: asio'。所以我無法做到這一點。無論如何,+1指出這是一個已知的錯誤。我可能會做的是ildjarn說,並更新我的boost庫。 – mfontanini

+1

我檢查了,顯然它是固定的版本1.48.0。但是它沒有說明哪個版本,所以我必須假定它是從版本1.48.0.3開始修復的(因爲你使用1.48.0.2進行了測試並且失敗了,而1.48.0.3是1.48.0版本中的最後一個小版本)。 –

+0

感謝您的檢查!我已經更新到最新版本,現在它工作正常。 – mfontanini