Skip to content

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

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.

Activity

tbaederr

tbaederr commented on Oct 18, 2024

@tbaederr
Contributor

template instantiation again I guess? @cor3ntin?

added
clang:frontendLanguage frontend issues, e.g. anything involving "Sema"
constexprAnything related to constant evaluation
and removed on Oct 18, 2024
llvmbot

llvmbot commented on Oct 18, 2024

@llvmbot
Member

@llvm/issue-subscribers-clang-frontend

Author: None (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](https://godbolt.org/z/d41PGojM9).)
#include &lt;string&gt;

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&lt;const char *&gt;' 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(&amp;"pear"[0], 4, std::allocator&lt;char&gt;())'
 4706 |     { return basic_string&lt;char&gt;{__str, __len}; }
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lit.cpp:7:3: note: in call to 'operator""s(&amp;"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.
cor3ntin

cor3ntin commented on Oct 18, 2024

@cor3ntin
Contributor

Yes, I think this is exactly #73232

frederick-vs-ja

frederick-vs-ja commented on May 30, 2025

@frederick-vs-ja
Contributor

Should we also close this a duplicate? @tbaederr @cor3ntin

tbaederr

tbaederr commented on May 30, 2025

@tbaederr
Contributor

Sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @cor3ntin@pkeir@EugeneZelenko@frederick-vs-ja@tbaederr

        Issue actions

          constexpr string literal: undefined function '_M_construct<const char *>' with libstdc++ · Issue #112894 · llvm/llvm-project