为什么 gcc 不能在解析第一个 return 子句后推断出 return 值类型?
Why can't gcc deduce the return value type after parsing the first return clause?
为什么此代码无效?
auto f() {
if (true) return 0;
return {};
}
解析0
后,我认为gcc应该知道return类型的函数f
是int
,但它仍然解释{}
作为解析最后的 return 子句时的 initializer_list
,为什么?
来自function#Return_type_deduction
if there are multiple return statements, they must all deduce to the same type
和
If the return statement uses a brace-init-list, deduction is not allowed:
禁止这种构造。
Once a return statement has been seen in a function, the return type deduced from that statement can be used in the rest of the function, including in other return statements.
只允许递归地重用函数。
为什么此代码无效?
auto f() {
if (true) return 0;
return {};
}
解析0
后,我认为gcc应该知道return类型的函数f
是int
,但它仍然解释{}
作为解析最后的 return 子句时的 initializer_list
,为什么?
来自function#Return_type_deduction
if there are multiple return statements, they must all deduce to the same type
和
If the return statement uses a brace-init-list, deduction is not allowed:
禁止这种构造。
Once a return statement has been seen in a function, the return type deduced from that statement can be used in the rest of the function, including in other return statements.
只允许递归地重用函数。