2011-04-01 48 views
1

我們已經編寫了一個智能指針類,並且已經使用它,並且通過內置的Visual Studio STL實現獲得了巨大成功。VS2008中帶有智能指針的STLPort模糊複製構造函數類

問題是我們意識到我們的性能瓶頸在於從Linux移植的代碼中STL庫(STL的使用方式明顯更快)。所以我試圖鏈接到STLPort,看它是否處理我們的性能問題。

當使用STLPort 5.2.1但是我得到非常奇怪的構建錯誤有關的ambigous複製構造函數。我已經剝離下來到50行的C++程序

#include "stdafx.h" 
#include <set> 

using namespace std; 

template<class T> 
class CRefCountPtr 
{ 
public: 
    CRefCountPtr(T* pT) : m_T(pT) 
    { 
    } 

    T** operator&() 
    { 
     return &m_T; 
    } 

    operator T*() const 
    { 
     return (T*)m_T; 
    } 

    bool operator< (T* pT) const 
    { 
     return m_T < pT; 
    } 

    T* m_T; 
}; 

class Example 
{ 
    int example; 
}; 


int _tmain(int argc, _TCHAR* argv[]) 
{ 

    set< CRefCountPtr<Example> > blah; 
    Example ex; 
    blah.insert(&ex); 

    return 0; 
} 

我回來從VS2008SP1的錯誤是

stlport\stl\_tree.h(318) : error C2782: 'void stlp_std::_Copy_Construct(_Tp *,const _Tp &)' : template parameter '_Tp' is ambiguous 
     stlport\stl\_construct.h(130) : see declaration of 'stlp_std::_Copy_Construct' 
     could be 'CRefCountPtr<T>' 
     with 
     [ 
      T=Example 
     ] 
     or  'Example *' 
     ..... 
     stlport_example.cpp(43) : see reference to class template instantiation 'stlp_std::set<_Key>' being compiled 
     with 
     [ 
      _Key=CRefCountPtr<Example> 
     ] 

我種停留在如何繼續在這裏,任何人有任何想法這個怎麼回事?

+0

在VS2010下編譯得很好。嘗試明確定義一個拷貝構造函數。 – ronag 2011-04-01 19:28:17

+0

雖然不能在g ++ 4.2下編譯。 – 2011-04-01 19:29:59

+2

哦,不,不,不,不,不。重載一元運算符&'是很可怕的。 :( – 2011-04-01 19:38:56

回答

1

它實際上是你的operator&這是造成歧義。在g ++中,歧義是在破壞而不是構造,但我認爲這是一個類似的問題。

編譯器會嘗試將您的T的地址構造/破壞它,並取回T**而不是CRefCountPtr<T>*,這會造成混淆。

我敢打賭,你可以創建自己的具體分配器,知道如何處理這個(又名不是模板)並讓它編譯。

從長期來看,更好的辦法是擺脫operator&,因爲這隻會導致混淆。

+0

我刪除了重載的'operator&',它處理了構建錯誤,再加上STLPort看起來會顯着改善我們的性能。太棒了! – pyromanfo 2011-04-01 20:51:14