我想要一个函数,它的返回类型将在函数内决定(取决于参数的值),但未能实现它。(可能是模板专业化?)
// 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)