How does noexcept in C++ change the assembly?

余生长醉 提交于 2020-02-02 11:18:04

问题


How does noexcept in C++ change the assembly? I tried a bit with small functions, in godbolt, but the assembly did not change.

float pi() 
//noexcept // no difference
{ return 3.14; }

int main(){
    float b{0};
    b = pi();
    return 0;
}

I am looking for a minimal working example, where I can see a change in the assembly due to noexcept.


回答1:


Pretty simple examples can be constructed that involve destructors directly rather than introspection on noexcept status:

void a(int);
void b() noexcept;
void c(int i) {
  struct A {
    int i;
    ~A() {a(i);}
  } a={i};
  b();
  a.i=1;
}

Here, the noexcept allows the initialization of a in the caller to be ignored, since the destructor cannot observe it.

struct B {~B();};
void f();
void g() noexcept {
  B b1;
  f();
  B b2;
}

Here, the noexcept allows the omission of frame information needed in case the callee throws. This depends on the (very common) decision to not unwind the stack when calling std::terminate.



来源:https://stackoverflow.com/questions/56782171/how-does-noexcept-in-c-change-the-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!