1

Suppose I have a foo function marked as noexcept somewhere in my code. Many other functions call foo, and many of them are also marked as noexcept.

Now suppose I have to adjust foo, and now it can throw an exception, so it's not noexcept anymore.

  1. All the functions that call foo must have the noexcept removed, is this correct?

  2. How am I suppose to find all those functions that call foo? Is there a better way than ordinary searching with my text editor?

rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
  • 1: Only if they don't handle the exception themselves. 2: Use an IDE – NathanOliver Aug 05 '20 at 21:28
  • 2
    1. *...handle the exceptions themselves* _or_ are happy to be terminated. 2. *Use an IDE* _or_ comment out the function and see what breaks - there are your callers (assuming no overloads / conversions etc allow it to compile). – Tony Delroy Aug 05 '20 at 21:37
  • @TonyDelroy *comment out the function and see what breaks* -- hey, that's one hell of a tip. Thank you. – rodrigocfd Aug 05 '20 at 21:42
  • 3
    Note that "comment out the function and see what breaks" is not a reliable way to find call sites in the presence of overloading, since callers may still compile by resolving to the second-best match. – Ben Voigt Aug 05 '20 at 21:51
  • @BenVoigt Yep, you're right. In addition to comment it out, I'm also using the features Visual C++ offers. – rodrigocfd Aug 05 '20 at 21:56
  • 1
    Visual Studio's Code Analysis tool will tell you if you're calling a non-`noexcept` function from a `noexcept` function. – ChrisMM Aug 05 '20 at 22:07
  • 1
    @BenVoigt: I did note that. – Tony Delroy Aug 05 '20 at 22:52

1 Answers1

0
  1. From searching on the spec, it seems that functions that call potentially-throwing functions, can in fact have the noexcept specifier. If an exception is thrown by foo and it's being called by another function which is marked as noexcept, then the exception will cause std::terminate to be called. So depending on if you want the exception to be propagated or not, you needn't remove all the noexcept specifiers. However, if you want the exception to be propagated then you probably do need to remove these noexcept specifiers.

  2. Off the top of my head, I think the best method would be searching using a text editor, although, perhaps someone else has a better method for refactoring. Also, an IDE can definitely handle major refactoring with ease.

NeelD
  • 73
  • 9