小编use*_*905的帖子

C ++函数返回类型作为模板

我想编写一个函数以根据参数的值返回不同的类对象。返回的类对象是模板化的。我将类名称作为函数的模板参数,

template< class T >
static T FindClassObjects(int var){
   if(var==1)
      return T<float>(var);
   else
      return T<int>(var);
Run Code Online (Sandbox Code Playgroud)

我可以这样做吗?

c++ templates class function return-type

6
推荐指数
1
解决办法
3415
查看次数

标签 统计

c++ ×1

class ×1

function ×1

return-type ×1

templates ×1