C++非具体类型的模板专业化(另一个模板类!)
我正在移植一些代码以在某些地方使用智能指针,并且遇到了专业化问题。在具体类型上专门化模板函数非常简单,但是如果我想在另一个模板化类(在我的例子中是 std::auto_ptr)上专门化特定方法怎么办?我似乎找不到合适的魔法词来让我的编译器不哭。
这是一个代码示例:
#include <memory> // std::auto_ptr
#include <iostream> // std::cout
class iSomeInterface
{
public:
virtual void DoSomething() = 0;
};
class tSomeImpl : public iSomeInterface
{
public:
virtual void DoSomething() { std::cout << "Hello from tSomeImpl"; }
};
template <typename T>
class tSomeContainer
{
public:
tSomeContainer(T& t) : _ptr(t){}
iSomeInterface* getInterfacePtr();
private:
T _ptr;
// usage guidelines
tSomeContainer();
};
template <typename T>
iSomeInterface* tSomeContainer<T>::getInterfacePtr()
{
return _ptr;
}
void testRegularPointer()
{
tSomeImpl* x = new tSomeImpl();
tSomeContainer<tSomeImpl*> xx(x);
xx.getInterfacePtr()->DoSomething();
delete x;
}
#if 1
// specialize for auto_ptr
template <typename T>
iSomeInterface* tSomeContainer< std::auto_ptr<T> >::getInterfacePtr()
{
return _ptr.get();
}
void testAutoPtr()
{
std::auto_ptr<tSomeImpl> y(new tSomeImpl);
tSomeContainer< std::auto_ptr<tSomeImpl> > yy(y);
yy.getInterfacePtr()->DoSomething();
}
#else
void testAutoPtr()
{
}
#endif
int main()
{
testRegularPointer();
testAutoPtr();
return 0;
}
当类型为 std::auto_ptr 时,我试图重写 tSomeContainer::getInterfacePtr() 方法,但我就是无法让它继续
I'm porting some code to use smart pointers in some places, and I ran into an issue with specialization. Specializing a template function on a concrete type is very straightforward, but what if I want to specialize a specific method on another templatized class (in my case, std::auto_ptr)? I can't seem to find the right magic words to make my compiler not cry.
Here's a code sample:
#include <memory> // std::auto_ptr
#include <iostream> // std::cout
class iSomeInterface
{
public:
virtual void DoSomething() = 0;
};
class tSomeImpl : public iSomeInterface
{
public:
virtual void DoSomething() { std::cout << "Hello from tSomeImpl"; }
};
template <typename T>
class tSomeContainer
{
public:
tSomeContainer(T& t) : _ptr(t){}
iSomeInterface* getInterfacePtr();
private:
T _ptr;
// usage guidelines
tSomeContainer();
};
template <typename T>
iSomeInterface* tSomeContainer<T>::getInterfacePtr()
{
return _ptr;
}
void testRegularPointer()
{
tSomeImpl* x = new tSomeImpl();
tSomeContainer<tSomeImpl*> xx(x);
xx.getInterfacePtr()->DoSomething();
delete x;
}
#if 1
// specialize for auto_ptr
template <typename T>
iSomeInterface* tSomeContainer< std::auto_ptr<T> >::getInterfacePtr()
{
return _ptr.get();
}
void testAutoPtr()
{
std::auto_ptr<tSomeImpl> y(new tSomeImpl);
tSomeContainer< std::auto_ptr<tSomeImpl> > yy(y);
yy.getInterfacePtr()->DoSomething();
}
#else
void testAutoPtr()
{
}
#endif
int main()
{
testRegularPointer();
testAutoPtr();
return 0;
}
I'm trying to override the tSomeContainer::getInterfacePtr() method when the type is a std::auto_ptr, but I just can't make it go
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能部分特化类模板的单个成员。 (您可以部分特化整个类模板,除了该成员之外具有相同的定义,但这通常并不有趣。)
相反,您可以使用重载函数模板:
You cannot partially specialize a single member of a class template. (You can partially specialize the whole class template, with identical definitions except for that one member, but that's usually not fun.)
Instead, you can use overloaded function templates:
对你有用吗
?
我似乎记得如果也为非模板类定义它,就会遇到上述问题。青年MMV
does
work for you?
I seem to remember running into issue with the above if it is also defined for non-template classes. YMMV