Skip to content

constexpr string literal: undefined function '_M_construct<const char *>' with libstdc++ #112894

Closed as duplicate of#73232
@pkeir

Description

@pkeir

The C++20 code below fails to compile under Ubuntu 24.04 with Clang trunk using clang++ -c -std=c++20 lit.cpp. In brief, using a string literal such as "mystring"s in a constexpr context fails when using libstdc++. (Compiler Explorer link is here.)

#include <string>

constexpr bool string_literal()
{
  using namespace std::literals;
  //std::literals::string_literals::operator""s("pear", 4); // no difference
  "pear"s; // short version of above ((both) constexpr since C++20)
  return true;
}

static_assert(string_literal());

The error message is below:

lit.cpp:11:15: error: static assertion expression is not an integral constant expression
   11 | static_assert(string_literal());
      |               ^~~~~~~~~~~~~~~~
/XXXX/YYYY/include/c++/15.0.0/bits/basic_string.h:635:2: note: undefined function '_M_construct<const char *>' cannot be used in a constant expression
  635 |         _M_construct(__s, __s + __n, std::forward_iterator_tag());
      |         ^
/XXXX/YYYY/include/c++/15.0.0/bits/basic_string.h:4706:14: note: in call to 'basic_string(&"pear"[0], 4, std::allocator<char>())'
 4706 |     { return basic_string<char>{__str, __len}; }
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lit.cpp:7:3: note: in call to 'operator""s(&"pear"[0], 4)'
    7 |   "pear"s; // short version of above ((both) constexpr since C++20)
      |   ^~~~~~~
lit.cpp:11:15: note: in call to 'string_literal()'
   11 | static_assert(string_literal());
      |               ^~~~~~~~~~~~~~~~
/XXXX/YYYY/include/c++/15.0.0/bits/basic_string.h:332:9: note: declared here
  332 |         _M_construct(_FwdIterator __beg, _FwdIterator __end,
      |         ^
1 error generated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationduplicateResolved as duplicatelibstdc++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