【发布时间】:2015-01-15 17:13:21
【问题描述】:
我正在尝试确定重载成员函数的返回类型,以便稍后在我的函数模板中使用该类型(参见下面的示例)。无法弄清楚如何使用 C++11 模板机制来做到这一点(不修改下面代码中 struct A 和 B 的定义)。这是否可行(一般在 C++11 中,特别是在 MSVS2013 中)以及如何实现?
struct A{};
struct B{};
struct X
{
double f(A&);
int* f(B&);
};
template<typename T, typename R = /*??? what X::f(T) returns ???*/>
R ff(T& arg)
{
X x;
R r = x.f(arg); // preferably if I can create local variables of type R here
return r;
}
int main()
{
A a; ff(a);
B b; ff(b);
}
【问题讨论】:
标签: c++ templates overloading return-type