c++: error with constexpr operator() [PR107939]
commite09bc034d1b4d692b409fa5af52ae34480a6f4dc
authorMarek Polacek <polacek@redhat.com>
Fri, 3 Mar 2023 16:24:24 +0000 (3 11:24 -0500)
committerMarek Polacek <polacek@redhat.com>
Tue, 7 Mar 2023 14:51:52 +0000 (7 09:51 -0500)
tree08a42ccc44decc40190c46b600d0f6945694b872
parent247cacc9e381d666a492dfa4ed61b7b19e2d008f
c++: error with constexpr operator() [PR107939]

Similarly to PR107938, this also started with r11-557, whereby cp_finish_decl
can call check_initializer even in a template for a constexpr initializer.

Here we are rejecting

  extern const Q q;

  template<int>
  constexpr auto p = q(0);

even though q has a constexpr operator().  It's deemed non-const by
decl_maybe_constant_var_p because even though 'q' is const it is not
of integral/enum type.

If fun is not a function pointer, we don't know if we're using it as an
lvalue or rvalue, so with this patch we pass 'any' for want_rval.  With
that, p_c_e/VAR_DECL doesn't flat out reject the underlying VAR_DECL.

PR c++/107939

gcc/cp/ChangeLog:

* constexpr.cc (potential_constant_expression_1) <case CALL_EXPR>: Pass
'any' when recursing on a VAR_DECL and not a pointer to function.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/var-templ74.C: Remove dg-error.
* g++.dg/cpp1y/var-templ77.C: New test.
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp1y/var-templ74.C
gcc/testsuite/g++.dg/cpp1y/var-templ77.C [new file with mode: 0644]