如何强制编译器为基类选择模板函数重载?
这是一个说明问题的例子
#include <iostream>
class A
{};
class B : public A
{};
template <class T>
void f (const T& t)
{
std::cout << "Generic f" << std::endl;
}
void f (const A& a)
{
std::cout << "Overload for A" << std::endl;
}
template <class T>
void call_f (const T& t)
{
f (t);
}
int main()
{
call_f (10);
call_f (A());
call_f (B());
return 0;
}
它产生输出
Generic f
Overload for A
Generic f
为什么编译器不拾取f (const A&)
在第三种情况下?UPD: 好的,这个已经很清楚了void f<B> (const B&)
比void f (const A&)
,但我仍在寻找第二个问题的答案。
是否有可能强迫它这样做不将 B 投射到 A?
Using call_f(B())
导致调用与模板版本最匹配的“f()”。对于非模板版本需要进行转换。结果,选择了模板。如果模板和非模板都是同样好的选择,那么非模板将是首选。
如果您想调用非模板,则需要将模板设置为非选项。例如,模板可以像这样实现
#include <type_traits>
template <class T>
typename std::enable_if<!std::is_base_of<A, T>::value>::type f(T const&)
{
std::cout << "Generic f\n";
}
如果无法使用 C++11,您可以实现以下版本std::is_base_of<...>
,使用来自的版本Boost http://www.boost.org/doc/libs/1_55_0/libs/type_traits/doc/html/index.html或使用简单的调度:
struct true_type {};
struct false_type {};
true_type A_is_base_of(A const*) { return true_type(); }
false_type A_is_base_of(void const*) { return false_type(); }
template <class T>
void f (T const&, false_type)
{
std::cout << "Generic f\n";
}
void f (A const&, true_type)
{
std::cout << "Overload for A\n";
}
template <class T>
void call_f (const T& t)
{
f (t, A_is_base_of(&t));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)