C++中的noexcept如何改变汇编?我在 godbolt 中尝试了一些小函数,但是 the assembly did not change .
float pi()
//noexcept // no difference
{ return 3.14; }
int main(){
float b{0};
b = pi();
return 0;
}
我正在寻找一个最小的工作示例,我可以在其中看到由于 noexcept
而导致的程序集更改。
最佳答案
非常简单的例子 can be constructed直接涉及析构函数而不是对 noexcept
状态进行内省(introspection):
void a(int);
void b() noexcept;
void c(int i) {
struct A {
int i;
~A() {a(i);}
} a={i};
b();
a.i=1;
}
这里,noexcept
允许忽略 caller 中 a
的初始化,因为析构函数无法观察到它。
struct B {~B();};
void f();
void g() noexcept {
B b1;
f();
B b2;
}
此处,noexcept
允许省略在 callee 抛出时所需的帧信息。这取决于调用 std::terminate
时不展开堆栈的(非常常见的)决定。
关于c++ - C++中的noexcept如何改变程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56782171/