[英]Use a function's return type as for another template function call
I'd like call a templated function with the typename being determined by another function's return type: 我想调用一个模板化函数,其typename由另一个函数的返回类型决定:
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;
}
}
EDIT: I cannot use C++11 :( 编辑:我不能使用C ++ 11 :(
In your specific case you'd better just rely on template type deduction instead of specifying it explicitly 在您的特定情况下,您最好只依靠模板类型推导而不是明确指定它
class Object { // Sample object
public:
template<typename T>
void call(T whatever, int x) { // Sample templated function (you didn't provide the code)
std::cout << "called call";
}
};
template<typename T>
void doSomething(T& value, int x)
{
Object obj;
if(getResult(x))
{ //Continue as normal if the result is true.
obj.call<T>(value, x);
}
else
{
obj.call(transformValue(value), x); // Type deduction
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.