https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113041

            Bug ID: 113041
           Summary: misleading diagnostic for variable of non-literal type
                    in constexpr function in C++20 mode
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ppalka at gcc dot gnu.org
  Target Milestone: ---

Here f<int>() is not constexpr in C++20 (but is in C++23 after P2242R3) due to
the local variable a of non-literal type A, so the testcase is invalid:

struct A { ~A(); };

void g();

template<class T>
constexpr int f() {
  if (__builtin_is_constant_evaluated())
    return 42;
  g();
  A a;
}

constexpr int n = f<int>();

But our diagnostic incorrectly blames the call to the non-constexpr g() for
f<int>() being non-constexpr, rather than the variable a:

constexpr-nonliteral.C:13:25: error: ‘constexpr int f() [with T = int]’ called
in a constant expression
   13 | constexpr int n = f<int>();
      |                   ~~~~~~^~
constexpr-nonliteral.C:6:15: note: ‘constexpr int f() [with T = int]’ is not
usable as a ‘constexpr’ function because:
    6 | constexpr int f() {
      |               ^
constexpr-nonliteral.C:9:4: error: call to non-‘constexpr’ function ‘void g()’
    9 |   g();
      |   ~^~
constexpr-nonliteral.C:3:6: note: ‘void g()’ declared here
    3 | void g();
      |      ^

Reply via email to