0

I am writing an iterator that should be very efficient and I was wondering whether I could declare operators operator->() and operator*() as noexcept since they will just call the same operators on the underlying pointer (but if the pointer is pointing to a non-available memory part, then the program is likely to crash)?

Vincent
  • 57,703
  • 61
  • 205
  • 388

1 Answers1

1

Why would you need to? Your compiler knows your code, and if you never throw within what you do in your operators, you don't get the exception handling overhead.

If you, however, do something inside these operators, that might throw, you semantically can't use the specifier (unless you want your program to immediately exit at throwing).

I personally don't think noexcept will yield any measurable performance boost in but the most obscure corner cases -- compilers that support it are producing backend code that has no performance penalty for code that doesn't throw. To me, noexcept is more of a language feature in the form of the compile-time checkable noexcept(expression) operator. Compare Bjarne's FAQ.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94