【发布时间】:2010-12-06 22:44:18
【问题描述】:
以下代码打印:
generic
overload
但我想要的是在这两种情况下都调用了重载或专业化,而不是通用的。我不想将重载与模板专业化混为一谈,它们在一起是因为没有一个能像我预期的那样工作。有没有什么模板魔法可以做到这一点?
#include <iostream>
class Interface {};
class Impl: public Interface {};
class Bar
{
public:
template<typename T> void foo(T& t) {
std::cout << "generic\n";
}
void foo(Interface& t) {
std::cout << "overload\n";
}
};
template<> void Bar::foo<Interface>(Interface& t) {
std::cout << "specialization\n";
}
int main() {
Bar bar;
Impl impl;
Interface& interface = impl;
bar.foo(impl);
bar.foo(interface);
return 0;
}
【问题讨论】:
标签: c++ templates inheritance