c++functionexceptionc++17

Is there a "safe" way to declare a lippincott function so that it won't be called outside of a catch?


A Lippincott function is a function which converts an exception into a return code that can be safely handled in a callback from another language which did not support exceptions, such as C. Such functions, because they deliberately use throw with no argument, thereby rethrowing the most recently caught exception, cannot be called outside the context of a catch block.

I am wondering if modern C++ (c++17 or c++20) has any declaration mechanism for specifically identifying such functions, and ensuring that a function is only ever called from the context of a catch block (or from the context of another such function), generating a compile time error if it is called from any other context.


Solution

  • There is no way to tell the compiler that a particular function should only be called from a catch handler.

    You can, however, call std::current_exception() from within your Lippincott function to determine if there currently an exception being handled. If this returns a nullptr then there is no exception active. You can then handle this at runtime by triggering an assert and/or having your function return (and not use throw).