C++ 编译器如何管理返回内联函数?
假设我有一个内联函数,如: 当然,当它到达C++ 编译器如何管理返回内联函数?,c++,function,compiler-construction,inline,C++,Function,Compiler Construction,Inline,假设我有一个内联函数,如: 当然,当它到达return语句时,必须忽略函数的其余部分(即inside1和inside2),因为return。但是如果我从父函数(Process())返回,它会立即返回,因此我永远看不到100 这意味着它以另一种方式进行 编译器如何处理这种情况?我试图创建一个代码块,但是return仍然返回到主函数…在编写“仿真”时,您忘记处理一个returns。在内联函数中,编译器会用goto语句替换它 void Process() { double value;
return
语句时,必须忽略函数的其余部分(即inside1
和inside2
),因为return
。但是如果我从父函数(Process()
)返回,它会立即返回,因此我永远看不到100
这意味着它以另一种方式进行
编译器如何处理这种情况?我试图创建一个代码块,但是return
仍然返回到主函数…在编写“仿真”时,您忘记处理一个return
s。在内联函数中,编译器会用goto
语句替换它
void Process() {
double value;
// begin of inlined function
// some condition
if (true) {
// some other condition
if (true) {
// another condition
if (true) {
value = 1.0;
goto next; // <<<<<<<<<<<<<<<<<<< return replaced by goto
}
// somethings
std::cout << "inside1" << std::endl;
}
// somethings
std::cout << "inside2" << std::endl;
}
value = 2.0;
next:
//end of inlined function
value *= 100.0;
std::cout << value << std::endl;
}
void进程(){
双重价值;
//内联函数的开始
//某些条件
如果(真){
//其他条件
如果(真){
//另一种情况
如果(真){
数值=1.0;
转到下一步;//在这种情况下,内联代码将把返回处理为转到,例如:
void Process() {
double value;
// some condition
if (true) {
// some other condition
if (true) {
// another condition
if (true) {
value = 1.0;
goto nextStep;
}
// somethings
std::cout << "inside1" << std::endl;
}
// somethings
std::cout << "inside2" << std::endl;
}
value = 2.0;
nextStep:
value *= 100.0;
std::cout << value << std::endl;
}
void进程(){
双重价值;
//某些条件
如果(真){
//其他条件
如果(真){
//另一种情况
如果(真){
数值=1.0;
转到下一步;
}
//有些事
显然,cout编译器不使用return,我不知道具体使用哪一个,但是还有很多其他选项,比如while(1){…break;}
或者甚至是goto
,或者一些更模糊的步骤,其结果是asminline
不会改变函数调用的语义,只会改变函数调用的链接。goto
对编程来说不是坏事吗?:)你认为if then else
的幕后黑手是什么?goto
本身并不坏(就像一把刀),只有它的用法可以是好的或坏的。@Jean BaptisteYunès paizza的评论可能是讽刺的。我是讽刺的:)谢谢你澄清这一点!
void Process() {
double value;
// begin of inlined function
// some condition
if (true) {
// some other condition
if (true) {
// another condition
if (true) {
value = 1.0;
goto next; // <<<<<<<<<<<<<<<<<<< return replaced by goto
}
// somethings
std::cout << "inside1" << std::endl;
}
// somethings
std::cout << "inside2" << std::endl;
}
value = 2.0;
next:
//end of inlined function
value *= 100.0;
std::cout << value << std::endl;
}
void Process() {
double value;
// some condition
if (true) {
// some other condition
if (true) {
// another condition
if (true) {
value = 1.0;
goto nextStep;
}
// somethings
std::cout << "inside1" << std::endl;
}
// somethings
std::cout << "inside2" << std::endl;
}
value = 2.0;
nextStep:
value *= 100.0;
std::cout << value << std::endl;
}