Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pkeir opened this issue Oct 18, 2024 · 3 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation

Comments

@pkeir
Copy link

pkeir commented Oct 18, 2024

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.
@tbaederr
Copy link
Contributor

template instantiation again I guess? @cor3ntin?

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation and removed new issue labels Oct 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2024

@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
Copy link
Contributor

Yes, I think this is exactly #73232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation
Projects
None yet
Development

No branches or pull requests

5 participants