Can constexpr functions contain try or throw, and what happens if a throwing path is taken during constant evaluation?
Show answer and explanation
Modern constexpr functions may contain control flow and a throw-expression, and C++20 permits try blocks in constexpr function bodies. A throw-expression is not a permitted result of a core constant expression, so if constant evaluation actually takes that path, the expression cannot initialize an object or satisfy a context that requires a constant expression and compilation fails there. The same constexpr function can execute at runtime and throw normally when called outside such a context. This lets one implementation enforce preconditions in both modes without implying that exceptions are caught during compile-time evaluation.
Modern constexpr functions may contain control flow and a throw-expression, and C++20 permits try blocks in constexpr function bodies. A throw-expression is not a permitted result of a core constant expression, so if constant evaluation actually takes that path, the expression cannot initialize an object or satisfy a context that requires a constant expression and compilation fails there. The same constexpr function can execute at runtime and throw normally when called outside such a context. This lets one implementation enforce preconditions in both modes without implying that exceptions are caught during compile-time evaluation.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.