Ton*_*roy 11
考虑...
auto f(int x)
{
if (x == 2)
return 3;
return 2.1;
}
Run Code Online (Sandbox Code Playgroud)
...这有一个模糊的回报类型 - int或double.显式返回类型 - 无论是前缀还是尾随 - 可以消除歧义并将return参数强制转换为返回类型.
如果你想使用尾随返回类型特别也很有用decltype,sizeof等上一些参数:
auto f(int x) -> decltype(g(x))
{
if (x == 2)
return g(x);
return 2;
}
Run Code Online (Sandbox Code Playgroud)