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

            Bug ID: 123738
           Summary: "is not a constant expression" in code using a string
                    literal operator template
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sberg.fun at gmail dot com
                CC: nshead at gcc dot gnu.org
  Target Milestone: ---

Starting with
<https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=c388d56a889469cf05a1512403e3267af7bb62be>
"c++: Fold non-ODR usages of potentially constant values early [PR120005]"
towards GCC 16,

> $ cat test.cc
> struct OStringLiteral {
>     constexpr OStringLiteral(char const *) {}
>     int str = 0;
> };
> 
> template<OStringLiteral L> struct OStringHolder {
>     static constexpr auto & literal = L;
> };
> 
> struct OString {
>     template<OStringLiteral L> constexpr OString(OStringHolder<L> const & 
> holder):
>         p(&holder.literal.str) {}
>     int const * p;
> };
> 
> template<OStringLiteral L> constexpr OStringHolder<L> operator ""_tstr() {
>     return OStringHolder<L>();
> }
> 
> constexpr OString s = ""_tstr;

started to fail with

> $ g++ -std=c++20 -fsyntax-only test.cc
> test.cc:20:23: error: ‘OString{(&<anonymous>.OStringLiteral::str)}’ is not a 
> constant expression
>    20 | constexpr OString s = ""_tstr;
>       |                       ^~~~~~~
> test.cc:20:23: error: ‘OString(operator""_tstr<OStringLiteral{0}>())’ is not 
> a constant expression because it refers to an incompletely initialized 
> variable

Reply via email to