[Bug libstdc++/105440] New: c++20: std::string's destructor not a constant expression when it should

janpmoeller at gmx dot de gcc-bugzilla@gcc.gnu.org
Sat Apr 30 13:28:52 GMT 2022


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

            Bug ID: 105440
           Summary: c++20: std::string's destructor not a constant
                    expression when it should
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janpmoeller at gmx dot de
  Target Milestone: ---

The following valid c++20 program fails to compile on both gcc and clang when
compiled with libstdc++:

/************************************************************/
#include <string>
#include <array>

constexpr auto foo(char c){
    std::array a{std::string{"foo"} + c};
    return true;
}

static_assert(foo('a'));
/************************************************************/

The emitted error is:

/************************************************************/
<source>:9:18: error: non-constant condition for static assertion
    9 | static_assert(foo('a'));
      |               ~~~^~~~~
In file included from
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/string:53,
                 from <source>:1:
<source>:9:18:   in 'constexpr' expansion of 'foo(97)'
<source>:7:1:   in 'constexpr' expansion of '(&
a)->std::array<std::__cxx11::basic_string<char>, 1>::~array()'
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/array:99:12:   in
'constexpr' expansion of
'<anonymous>->std::__cxx11::basic_string<char>::~basic_string()'
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/bits/basic_string.h:795:19:
  in 'constexpr' expansion of
'((std::__cxx11::basic_string<char>*)this)->std::__cxx11::basic_string<char>::_M_dispose()'
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/bits/basic_string.h:275:26:
error: '((& a) == <anonymous>)' is not a constant expression
  275 |       { return _M_data() == _M_local_data(); }
      |                ~~~~~~~~~~^~~~~~~~~~~~~~~~~~
Compiler returned: 1
/************************************************************/

The same program compiles with libc++, and on msvc. Also see the following
godbolt link for comparison:
https://godbolt.org/z/zfn1Pr7P1


More information about the Gcc-bugs mailing list