gcc如何实现C++中函数auto返回类型推导 一、问题 二、gcc的做法 三、C++标准的说明 四、测试代码 五、变量auto的推导

在新的C++标准中,auto的应用范围越来越广,但是比较常用的典型场景在于使用一个auto接收一个函数的返回值。问题是对于一个函数的auto返回值,这个返回值是如何确定的?特别是一个函数有多个返回值的时候。
直观上的理解是当一个函数体(function body)解析(parse)完成之后,遍历它的所有语句(statement),并找到其中的return语句,逐个推导它们的返回值。

二、gcc的做法

从代码上看,gcc的这个解析是在函数体解析的时候同步进行,而不是在整个函数体解析完成之后再事后遍历。好在从这个代码看其实也比较直观,就是在遇到return语句的时候进行特殊处理,将这个return之后表达式的类型和当前解析函数。在假设return必定出现在函数体内的前提下,必定可以将这个表达式的类型赋值给当前函数(current_function_decl全局变量)的返回值中。
gcc-10.1.0gcccp ypeck.c
cp_parser_jump_statement==>>finish_return_stmt==>>check_return_expr
tree
check_return_expr (tree retval, bool *no_warning)
{
……
functype = TREE_TYPE (TREE_TYPE (current_function_decl));

/* Deduce auto return type from a return statement. */
if (FNDECL_USED_AUTO (current_function_decl))
{
tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
……
if (type == error_mark_node)
/* Leave it. */;
else if (functype == pattern)
apply_deduced_return_type (current_function_decl, type);
else if (!same_type_p (type, functype))
{
if (LAMBDA_FUNCTION_P (current_function_decl))
error_at (loc, "inconsistent types %qT and %qT deduced for "
"lambda return type", functype, type);
else
error_at (loc, "inconsistent deduction for auto return type: "
"%qT and then %qT", functype, type);
}
functype = type;
}
……
}

三、C++标准的说明

这个标准比较冗长一些,当然也比较详细和精确 

四、测试代码


tsecer@harry: cat c++.lambda.return.type.deduce.cpp
auto foo()
{
return 1;
}

int main(int argc, const char * argv[])
{
auto xxx = [](int x)
{
if (x > 10)
{
return x - 10;
}
return x;
};

return xxx(argc) + foo();
}
tsecer@harry: gcc -std=c++14 c++.lambda.return.type.deduce.cpp
tsecer@harry:

五、变量auto的推导

我记得之前看过是通过类似于模板的推导规则实现的。