Skip to content

c++20/libstdc++: char_traits.h:213:13: note: subexpression not valid in a constant expression #58754

@timblechmann

Description

@timblechmann

using this constexpr boost static_string (requires static_string develop) does not compile with clang against libstdc++ (g++ compiles against libstdc++ also clang++ compiles against libc++).

#include <boost/static_string.hpp>
using str = boost::static_strings::static_string<38>;
constexpr str a{ "mystr" };
c.cpp:5:15: error: constexpr variable 'a' must be initialized by a constant expression
constexpr str a{ "mystr" };
              ^~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/char_traits.h:213:13: note: subexpression not valid in a constant expression
   if (__s1 == __s2)
            ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/char_traits.h:417:11: note: in call to 'move(&a.static_string_base::data_[0], &"mystr"[0], 5)'
   return __gnu_cxx::char_traits<char_type>::move(__s1, __s2, __n);
          ^
/home/tim/NIBuild/3rdparty/boost-v1.78.0-R8/boost/static_string/static_string.hpp:6000:3: note: in call to 'move(&a.static_string_base::data_[0], &"mystr"[0], 5)'
  traits_type::move(data(), s, size());
  ^
/home/tim/NIBuild/3rdparty/boost-v1.78.0-R8/boost/static_string/static_string.hpp:1413:12: note: in call to '&a->assign(&"mystr"[0], 5)'
    return assign(s, traits_type::length(s));
           ^
/home/tim/NIBuild/3rdparty/boost-v1.78.0-R8/boost/static_string/static_string.hpp:961:5: note: in call to '&a->assign(&"mystr"[0])'
    assign(s);
    ^
c.cpp:5:15: note: in call to 'basic_static_string(&"mystr"[0])'
constexpr str a{ "mystr" };
              ^
1 error generated.

the offending code in libstdc++ is this one:

	  if (__s1 == __s2) // unlikely, but saves a lot of work
	    return __s1;

https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/char_traits.h#L216-L217

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"libstdc++GNU libstdc++ C++ standard library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions