1

我们编写了一个智能指针类,并通过内置的 Visual Studio STL 实现成功地使用它。

问题是我们已经意识到我们的性能瓶颈存在于从 Linux 移植的代码中的 STL 库中(其中 STL 比我们使用它的方式快得多)。所以我试图在 STLPort 中链接,看看它是否能解决我们的性能问题。

但是,当使用 STLPort 5.2.1 时,我会遇到与模棱两可的复制构造函数相关的非常奇怪的构建错误。我把它精简为一个 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>
        ]

我有点卡在如何在这里进行,有人知道这是怎么回事吗?

4

1 回答 1

1

实际上是你operator&造成了歧义。在 g++ 中,歧义是破坏而不是构造,但我认为这是一个类似的问题。

编译器尝试获取您的 T 的地址来构造/破坏它,并取回 aT**而不是 a CRefCountPtr<T>*,从而造成混乱。

我敢打赌,您可以创建自己的特定分配器,该分配器知道如何处理此问题(也不是模板)并对其进行编译。

可能更好的长期是摆脱它,operator&因为它只会引起混乱。

于 2011-04-01T19:36:19.783 回答