小编Aus*_*ser的帖子

根据参数返回类型

我想要一个函数,它的返回类型将在函数内决定(取决于参数的),但未能实现它。(可能是模板专业化?)

// half-pseudo code
auto GetVar(int typeCode)
{
  if(typeCode == 0)return int(0);
  else if(typeCode == 1)return double(0);
  else return std::string("string");
}
Run Code Online (Sandbox Code Playgroud)

我想在不指定类型的情况下使用它:

auto val = GetVar(42); // val's type is std::string
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming c++11

5
推荐指数
1
解决办法
1650
查看次数

标签 统计

c++ ×1

c++11 ×1

metaprogramming ×1

templates ×1