简体   繁体   English

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

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

We've written a smart pointer class and have been using it with great success with the built-in Visual Studio STL implementation. 我们已经编写了一个智能指针类,并且已将其与内置的Visual Studio STL实现一起成功使用。

The problem is we've realized our performance bottlenecks are in the STL library in code ported from Linux (where the STL is significantly faster the way we're using it). 问题在于,我们已经意识到性能瓶颈在Linux移植的STL库中(其中STL使用我们的方式要快得多)。 So I'm trying to link in STLPort to see if it deals with our performance problems. 因此,我尝试在STLPort中进行链接,以查看它是否能够解决我们的性能问题。

When using STLPort 5.2.1 however I get very strange build errors related to ambigous copy constructors. 但是,当使用STLPort 5.2.1时,我得到了与模棱两可的复制构造函数有关的非常奇怪的构建错误。 I've stripped it down to a 50 line C++ program 我将其剥离为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;
}

The error I get back from VS2008SP1 is 我从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>
        ]

I'm kind of stuck at how to proceed here, anybody have any idea what's going on with this one? 我有点想知道如何进行此操作,任何人都不知道这是怎么回事?

It's actually your operator& that's causing ambiguity. 实际上是由您的operator&引起的歧义。 In g++ the ambiguity is in destruct rather than construct but I assume it's a similar problem. 在g ++中,歧义是破坏而不是构造,但我认为这是一个类似的问题。

The compiler tries to take the address of your T to construct/destruct it, and gets back a T** instead of a CRefCountPtr<T>* , causing confusion. 编译器尝试获取您的T的地址来构造/销毁它,然后取回T**而不是CRefCountPtr<T>* ,从而造成混乱。

I bet you could create your own specific allocator that knows how to deal with this (aka not a template) and get it to compile. 我敢打赌,您可以创建自己的特定分配器,该分配器知道如何处理该分配器(也称为非模板)并进行编译。

Probably better long term is to get rid of the operator& as it'll only cause confusion. 长期来看可能更好的方法是摆脱operator&因为这只会造成混乱。

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

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