【发布时间】:2015-05-29 18:31:04
【问题描述】:
我想调用一个模板函数,其类型名由另一个函数的返回类型决定:
template<typename T>
void doSomething(T& value, int x)
{
if(getResult(x)) // Continue as normal if the result is true.
{
object.call<T>(value, x);
}
else
{
//I'd like to have this function be templated on the
//return type of the transformValue function. This
//transformValue functions takes in a value of some type
//and then transforms it to some value of other type.
object.call<return_type_of_transform_value>(transformValue(value), x);
}
}
// Some condition
bool getResult(int x)
{
if(x == 42)
{
return true;
}
else
{
return false;
}
}
编辑:我不能使用 C++11 :(
【问题讨论】:
-
你有多个模板化的 transformValue 函数还是什么?
-
@MarcoA.:是的,transformValue 也是模板化的。
-
函数不能自然派生模板类型吗?通常你不应该为函数指定模板参数,而是让模板参数推导为你工作。
-
@NathanOliver:我不得不改变一些东西,而推论确实奏效了!谢谢。