https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80061
Bug ID: 80061 Summary: error on constexpr function with an unevaluated throw Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The following valid test case was reduced from the invalid one submitted with bug 70377. GCC rejects the constexpr function simple_find because it contains a throw statement even though the statement is not evaluated in the (constexpr) contexts the function is called. Clang 5.0 and EDG eccp 4.13 both accept the program. $ cat t.C && gcc -Wall -Wextra -Wpedantic t.C struct array { int a [3]; constexpr const int& operator[](int i) const { return a[i]; } }; constexpr int simple_find (const array &a, int x) { for (int i = 0; i != 3; ++i) if (a[i] == x) return i; throw; } static constexpr array some_ints { { 10, 11 } }; static_assert (simple_find (some_ints, 10) == 0, "#1"); static_assert (simple_find (some_ints, 11) == 1, "#2"); t.C: In function ‘constexpr int simple_find(const array&, int)’: t.C:14:3: error: expression ‘<throw-expression>’ is not a constant expression throw; ^~~~~ t.C: At global scope: t.C:19:1: error: non-constant condition for static assertion static_assert (simple_find (some_ints, 10) == 0, "#1"); ^~~~~~~~~~~~~ t.C:19:28: error: ‘constexpr int simple_find(const array&, int)’ called in a constant expression static_assert (simple_find (some_ints, 10) == 0, "#1"); ~~~~~~~~~~~~^~~~~~~~~~~~~~~ t.C:20:1: error: non-constant condition for static assertion static_assert (simple_find (some_ints, 11) == 1, "#2"); ^~~~~~~~~~~~~ t.C:20:28: error: ‘constexpr int simple_find(const array&, int)’ called in a constant expression static_assert (simple_find (some_ints, 11) == 1, "#2"); ~~~~~~~~~~~~^~~~~~~~~~~~~~~