让说,我有一个内联函数像this:编译器如何管理返回内联函数?
inline double CalculateValue() {
// some condition
if (true) {
// some other condition
if (true) {
// another condition
if (true) {
return 1.0;
}
// somethings
std::cout << "inside1" << std::endl;
}
// somethings
std::cout << "inside2" << std::endl;
}
return 2.0;
}
void Process() {
double value = CalculateValue();
value *= 100.0;
std::cout << value << std::endl;
}
int main()
{
Process();
}
,将“复制和粘贴”的CalculateValue()
函数内的Process()
之一。正如预期的那样,结果是100
。
但是,如果我尝试emulate如何“复制和粘贴”将执行,有件事情我不明白:
void Process() {
double value;
// some condition
if (true) {
// some other condition
if (true) {
// another condition
if (true) {
value = 1.0;
return;
}
// somethings
std::cout << "inside1" << std::endl;
}
// somethings
std::cout << "inside2" << std::endl;
}
value = 2.0;
value *= 100.0;
std::cout << value << std::endl;
}
int main()
{
Process();
}
当然,当它到达return
声明,该函数的其余部分必须被忽略(即inside1
和inside2
绝不能被打印),因为return
。但是,如果我从0123.父功能(Process()
),它立即返回,所以我永远不会看到100
。
这意味着它以另一种方式。
编译器如何管理这种情况?我试图创建一个代码块,但仍然return
返回到主函数...
编译器显然不使用return,我不知道确切使用了哪一个,但有很多其他选项,例如' while(1){... break; }'甚至'goto'或者一些比较模糊的步骤会导致asm – slawekwin
'inline'不会改变函数调用的语义,只会导致链接。 – sp2danny