【发布时间】:2021-01-29 06:40:01
【问题描述】:
我想从不同的类家族中获得一个共同的属性。对于一些人,我可以只使用一个成员,对于一些人我需要调用一个函数。
我能否以某种方式重载模板函数,以便编译器根据类是否具有functionA、functionB 或member 来选择合适的模板函数?目前我收到错误,因为我多次定义相同的模板函数...
template<class TypeWithFunctionA>
int doSomething(const TypeWithFunctionA & h)
{
return h.functionA();
}
template<class TypeWithFunctionB>
int doSomething(const TypeWithFunctionB & h)
{
return h.functionB();
}
template<class TypeWithMember>
int doSomething(const TypeWithMember & h)
{
return h.member;
}
【问题讨论】:
标签: c++ templates overloading