<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/142144>142144</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`constexpr string` from `constexpr string_view` is not always a constant expression
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
fekir
</td>
</tr>
</table>
<pre>
Consider following program, which compiles with gcc and msvc
~~~~
#include <string_view>
#include <string>
template <typename T>
constexpr int foo() {
constexpr auto test = std::string_view( "test" );
static_assert(test.size() >1);
static_assert(std::string(test).size() >1); // should not fail
return 0;
}
int bar(){
return foo<int>();
}
~~~~
With `clang` it fails to compile with "error: static assertion expression is not an integral constant expression".
Strangely, adding a local `char` array and a `static_assert`, changes the behavior
This code still fails to compile
~~~~
#include <string_view>
#include <string>
template <typename T>
constexpr int foo() {
constexpr auto test = std::string_view( "test" );
constexpr char test2[] = "test";
static_assert(test.size() >1);
static_assert(std::string(test).size() >1); // should still not fail
static_assert(std::string(test2).size() >1);
return 0;
}
int bar(){
return foo<int>();
}
~~~~
This one compiles:
~~~~
#include <string_view>
#include <string>
template <typename T>
constexpr int foo() {
constexpr auto test = std::string_view( "test" );
constexpr char test2[] = "test";
static_assert(test.size() >1);
static_assert(std::string(test2).size() >1);
static_assert(std::string(test).size() >1); // success
return 0;
}
int bar(){
return foo<int>();
}
~~~~
live demo: https://godbolt.org/z/KK1WjajPq
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVsGO2zYQ_ZrRZRBDGlqWddDBuxtfcinQADkGNDWSuKVIl6Tsbg777QUle3eTbIoW2PZQ5GTBM_PmzbwnUjIE3VvmBsobKO8yOcXB-abj37TPDq59aG6dDbplj50zxp217fHoXe_lCHSL50GrAZUbj9pwwLOOA_ZKobQtjuGkIN9Bvnt8fHxMTyS0VWZqGUHchui17T-fNJ9BvP9B-BLJd5HHo5FxDsWHI1s5Mn5cwsrZEPmPo0dtI3bOAW2BaoTqBvId4nNcTtFh5BARxB2G2ILYgdi9pEJbBKKUA0QIVIO4WSgghiijVp9lCOwj0DZlrYL-wteG4n3xVPFd9jftLvVA9esQCLQH2mMY3GRatC5iJ7WZiXiOk7eYX7hVdwtDyHdpAwfpF7TrAp4K0m7ErbYxLW5JeYFwlSnffUo6wiZXRtoeNjnqpXnA6K5qL2IDEXvvPIjrwLgMrJ3FtHMOIT3qME8gbdKIey_NIou08UUaEK0WBr9GL23P5iG5TLZt8p1E45Q0M7FB-sRLei8fZrfJ9PfXK9_kqVgNCShgHBgPPMiTdn7p8XHQAZVrGUPUxnw34f_GvC9R0uZmFFpe-RnsuejZ7f_M7G_s9kWQrzz_9xrQDzskkH_7vZkd5Sw_nYiJ4U8XvbWL_lrkN7PipBSH8J8cuEafGFseXTpHhxiPs3NmHr1rD87ElfM90P4L0P7Dh-LTvbz_5fesbURbi1pm3BTVerspiqKkbGjKalOKbau6Q67Kuqi32-2mkx2XVd0V1abKdEM5lXkp8kIUdUGrqqpF1ZU1FeuqFkUN65xHqc3KmNOYemc6hImbYk3Fep0ZeWAT5q8GIstnnKNJ-fIu800qeneY-gDr3OgQwzNM1NFwk47wJzNdZNnk2Hk34iuxxZfpHrpcI-YsHwLK1-6QbPKm-WaFOg7TYaXcCLRPTC4_747e3bOKQPuZfwDaXwY8NfRnAAAA__8Nlc-y">