Why is C++0x's `noexcept` checked dynamically?

后端 未结 6 1573
抹茶落季
抹茶落季 2020-12-14 15:59

I am curious about the rationale behind noexcept in the C++0x FCD. throw(X) was deprecated, but noexcept seems to do the same thing. I

6条回答
  •  臣服心动
    2020-12-14 16:53

    If I remember throw has been deprecated because there is no way to specify all the exceptions a template function can throw. Even for non-template functions you will need the throw clause because you have added some traces.

    On the other hand the compiler can optimize code that doesn't throw exceptions. See "The Debate on noexcept, Part I" (along with parts II and III) for a detailed discussion. The main point seems to be:

    The vast experience of the last two decades shows that in practice, only two forms of exceptions specifications are useful:

    • The lack of an overt exception specification, which designates a function that can throw any type of exception:

      int func(); //might throw any exception
      
    • A function that never throws. Such a function can be indicated by a throw() specification:

      int add(int, int) throw(); //should never throw any exception
      

提交回复
热议问题