[英]Use a function's return type as for another template function call
我想調用一個模板化函數,其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;
}
}
編輯:我不能使用C ++ 11 :(
在您的特定情況下,您最好只依靠模板類型推導而不是明確指定它
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.