5

I understand containers can optimize for types having noexcept move constructor / assignment / swap. But are there practical reasons (aside documentation) for specifying other operations as noexcept?

Specifically, I'm interested if there are optimizations in the standard library to be gained by additionally having noexcept:

  1. Default constructor
  2. Copy constructor
  3. Copy assignment
Valentin Milea
  • 3,186
  • 3
  • 28
  • 29
  • 1
    Note that you can use the `noexcept()` compile-time expression to in your own code to take different code paths depending on whether or not some expression throws, so it's not just standard library containers that can make use of this information. – cdhowie Jul 16 '15 at 16:51
  • I hear some people don't like to put `noexcept` on regular methods that don't throw because you might want to use the exception mechanism in your test framework. I think that logic is applied to the standard library: you'll see `noexcept` mainly on default constructors, move constructors and specializations of `std::swap`, and not much else – KABoissonneault Jul 16 '15 at 16:57
  • You may find some of the details in [my answer here](http://stackoverflow.com/a/30225086/1708801) helpful. – Shafik Yaghmour Jul 16 '15 at 16:58
  • @cdhowie Of course you are right, but my focus now is on advertising this trait rather than consuming it. – Valentin Milea Jul 16 '15 at 16:58
  • @Taekahn The other question doesn't consider possible overhead of std::terminate() – Valentin Milea Jul 16 '15 at 16:59
  • "If a noexcept function throws then std::terminate is called which seems like it would involve a small amount of overhead"… No, this should be implemented by not generating exception tables for such a function, which the exception dispatcher should catch and then bail out. – Potatoswatter May 29 '12 at – Taekahn Jul 16 '15 at 17:01
  • @Taekahn Right, point 2 is already answered. But the accepted answer said it was too early for "best practices", maybe things have changed in 3 years. Also I'm interested in tagging move vs. copy constructors. – Valentin Milea Jul 16 '15 at 17:22
  • If you read the 2nd highest rated answer, it addresses your point 1 in the best way it can possibly be expressed. – Taekahn Jul 16 '15 at 17:29
  • 1
    @ValentinMilea: if something changed during the 3 years, I think updates should be done as edits / comments / new answers to previous question. – GingerPlusPlus Jul 16 '15 at 17:34
  • I will refocus the question. – Valentin Milea Jul 16 '15 at 17:55

0 Answers0