Duplicate of#73232
Description
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
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
tbaederr commentedon Oct 18, 2024
template instantiation again I guess? @cor3ntin?
llvmbot commentedon Oct 18, 2024
@llvm/issue-subscribers-clang-frontend
Author: None (pkeir)
The error message is below:
cor3ntin commentedon Oct 18, 2024
Yes, I think this is exactly #73232
constexpr string
fromconstexpr string_view
is not always a constant expression #142144frederick-vs-ja commentedon May 30, 2025
Should we also close this a duplicate? @tbaederr @cor3ntin
tbaederr commentedon May 30, 2025
Sure.