繁体   English   中英

带有智能指针类的VS2008中的STLPort模糊副本构造函数

[英]STLPort ambiguous copy constructor in VS2008 with smart pointer class

我们已经编写了一个智能指针类,并且已将其与内置的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>
        ]

我有点想知道如何进行此操作,任何人都不知道这是怎么回事?

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

编译器尝试获取您的T的地址来构造/销毁它,然后取回T**而不是CRefCountPtr<T>* ,从而造成混乱。

我敢打赌,您可以创建自己的特定分配器,该分配器知道如何处理该分配器(也称为非模板)并进行编译。

长期来看可能更好的方法是摆脱operator&因为这只会造成混乱。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM