VS2008中带有智能指针类的STLPort不明确复制构造函数
我们编写了一个智能指针类,并通过内置的 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>
]
我有点不知道如何继续这里,有人知道这个程序是怎么回事吗?
We've written a smart pointer class and have been using it with great success with the built-in Visual Studio STL implementation.
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). So I'm trying to link in STLPort to see if it deals with our performance problems.
When using STLPort 5.2.1 however I get very strange build errors related to ambigous copy constructors. I've stripped it down to a 50 line C++ program
#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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上是您的
operator&
导致了歧义。在 g++ 中,歧义在于析构而不是构造,但我认为这是一个类似的问题。编译器尝试使用 T 的地址来构造/析构它,并返回
T**
而不是CRefCountPtr*
,从而导致混乱。我打赌您可以创建自己的特定分配器,它知道如何处理这个问题(也称为不是模板)并使其编译。
从长远来看,可能更好的办法是摆脱
operator&
因为它只会引起混乱。It's actually your
operator&
that's causing ambiguity. In g++ the ambiguity is in destruct rather than construct but I assume it's a similar problem.The compiler tries to take the address of your T to construct/destruct it, and gets back a
T**
instead of aCRefCountPtr<T>*
, causing confusion.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.