int main()
{
int f=fun();
...
}
int fun()
{
return 1;
return 2;
}在上面的程序中,当从主函数调用一个函数并且函数包含两个返回语句时,
发布于 2018-06-25 13:03:57
程序控制永远不会到达return 2;,优化的编译器将删除它。
我所知道的唯一一种足够疯狂的语言是能够达到两个return语句的Java:
try {
return 1; // this is evaluated
} finally {
return 2; // this is the one actually returned
}发布于 2018-06-25 13:15:57
如果您还记得返回语句对应于程序集操作代码(“从子例程返回”),该操作代码设置了程序计数器的值,并在调用后使用指令的地址,那么很明显,第二个返回语句永远不会执行。
https://stackoverflow.com/questions/51024142
复制相似问题