繁体   English   中英

使用函数的返回类型作为另一个模板函数调用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM