<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/161071>161071</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
The constructor `std::string(std::string_view)` from libstdc++ can't be evaluated at compile-time
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
HolyBlackCat
</td>
</tr>
</table>
<pre>
When using libstdc++, the following refuses to evaluate at compile-time: (in Clang, but not in GCC)
```cpp
#include <string>
#include <string_view>
constexpr std::size_t i = std::string(std::string_view("42")).size();
```
With the following error:
```console
<source>:4:23: error: constexpr variable 'i' must be initialized by a constant expression
4 | constexpr std::size_t i = std::string(std::string_view("42")).size();
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/basic_string.h:692:2: note: undefined function '_M_construct<const char *>' cannot be used in a constant expression
692 | _M_construct(__s, __s + __n, std::forward_iterator_tag());
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/basic_string.h:193:9: note: in call to 'basic_string(&"42"[0], 2, std::allocator<char>())'
193 | : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/basic_string.h:888:4: note: in call to 'basic_string({{2, &"42"[0]}}, std::allocator<char>())'
888 | : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:4:27: note: in call to 'basic_string<std::basic_string_view<char>, void>(std::string_view("42"), std::allocator<char>())'
4 | constexpr std::size_t i = std::string(std::string_view("42")).size();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/bits/basic_string.h:343:9: note: declared here
343 | _M_construct(_FwdIterator __beg, _FwdIterator __end,
| ^
1 error generated.
```
GCC has no problem with this, so I'm not sure where it needs to be fixed, in Clang or in libstdc++.
Tested on Clang 21 with libstdc++ 15: https://gcc.godbolt.org/z/8Y3s87Y1G
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUVk2PGjkT_jXmUqLVbUN_HDhAT8ibQ05vpCgny90uaO8aG9lumOSwv31lAzNAMisirXY3rRYfZVeV63mqXCW8V1uDuCDzFZk_TcQYBusW_7P660qL_vdWhEln5dfF5wENjF6ZLWjV-SB7QlfpbSEMCBurtT3GZYeb0aOHYAEPQo8iIIgAvd3tlcZpUDskbAmE1spAq4XZRhvdGMDYAMrA-7YltCH5kpT56e33-_iXMmV6PUoEwlofnDJbwt69scIPCo-n5d4aH_B578AHSdiSsKVX35AHUEDY05X0ZJPWd5KTLVoTSmeUUBqPR5ssGknShrDV9XlJvvyswnAHDDpnXTR7HZk13mqMItZ6O7oe45nZckbYkrII1EUNXsM4CKdEpxEIrRShFexGH6BDUEYFJbT6hhK6ryBOOsIEiHrovbKG5EsAgBmQqoV_ChpIT_RI5u_gj4efRO7a7gOh63MGuSk-77V16Ahdb_t-6o3Y-8HGLVp1JyGh6-e65OVsqpUZn6dbMxK6Lsosz3JC11n2_cc5g6KjS2q_KnQq-PglvOr5KfJsIGxZNjQSFekxNqTEHo3EjTIoYTOaPihrIk38I09Yu7EPhLXpN_SDcEDoMlJOK-iFiSXQIYweZSyFtwksG5rgvDw39mnNuY9VxbkHQlfAuYl_X7jbWHcUTnIV0IlgHQ9ie6brh4z912komlgqzTUNykAvtI7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