Bug 80061 - error on constexpr function with an unevaluated throw
Summary: error on constexpr function with an unevaluated throw
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 9.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 82572 83242 86192 86207 (view as bug list)
Depends on:
Blocks: 82572
  Show dependency treegraph
 
Reported: 2017-03-16 01:40 UTC by Martin Sebor
Modified: 2023-12-30 20:47 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-10-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2017-03-16 01:40:34 UTC
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");
                ~~~~~~~~~~~~^~~~~~~~~~~~~~~
Comment 1 Matthijs van Duin 2017-07-21 06:12:50 UTC
Simpler test case:

void foo( bool ok ) {
    if( ok )
        return;
    throw;
}

fails to compile with g++ 6.3.0 and g++ 7.1.0
Comment 2 Matthijs van Duin 2017-07-21 06:16:51 UTC
> void foo( bool ok ) {
 ^constexpr
Comment 3 Paolo Carlini 2017-10-17 08:26:50 UTC
*** Bug 82572 has been marked as a duplicate of this bug. ***
Comment 4 Martin Sebor 2017-12-01 19:43:51 UTC
*** Bug 83242 has been marked as a duplicate of this bug. ***
Comment 5 Jonathan Wakely 2018-06-18 11:26:47 UTC
*** Bug 86192 has been marked as a duplicate of this bug. ***
Comment 6 Jonathan Wakely 2018-06-19 10:35:48 UTC
*** Bug 86207 has been marked as a duplicate of this bug. ***
Comment 7 Marek Polacek 2019-08-08 16:44:03 UTC
Comment 1 compiles fine since r264171.
Comment 8 Andrew Pinski 2021-08-28 21:25:24 UTC
Fixed.