<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141563>141563</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
consteval and constexpr static variable do not work well together
</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 example
----
#include <iostream>
#include <string_view>
#define USECONSTEVAL
#ifdef USECONSTEVAL
#define CEXPR consteval
#else
#define CEXPR constexpr
#endif
CEXPR std::string_view name(){
return __PRETTY_FUNCTION__;
}
CEXPR std::string_view name2(){
constexpr auto function = std::string_view(__PRETTY_FUNCTION__);
constexpr static auto value = [] <std::size_t...Idxs>(std::string_view str, std::index_sequence<Idxs...>)
{
return std::array{str[Idxs]..., '\0'};
}(function, std::make_index_sequence<function.size()>{});
static_assert(!std::string_view(value).empty());
return std::string_view(value);
}
template <std::size_t...Idxs>
constexpr auto substring_as_array(std::string_view str, std::index_sequence<Idxs...>)
{
return std::array{str[Idxs]..., '\0'};
}
CEXPR std::string_view name3(){
constexpr auto function = std::string_view(__PRETTY_FUNCTION__);
constexpr static auto value = substring_as_array(function, std::make_index_sequence<function.size()>{});
static_assert(!std::string_view(value).empty());
return std::string_view(value);
}
struct helper {
static constexpr std::string_view value = name();
};
CEXPR std::string_view name4(){
constexpr auto value = helper::value;
return std::string_view{value.data(), value.size()};
}
int main(){
static_assert(not name().empty());
std::cout << "__PRETTY_FUNCTION__: \"" << name() << "\"\n";
static_assert(not name2().empty());
std::cout << "__PRETTY_FUNCTION__: \"" << name2() << "\"\n";
static_assert(not name3().empty());
std::cout << "__PRETTY_FUNCTION__: \"" << name3() << "\"\n";
static_assert(not name4().empty());
std::cout << "__PRETTY_FUNCTION__: \"" << name4() << "\"\n";
}
----
by toggling the macro `USECONSTEVAL`, the output of this sample program changes from
----
__PRETTY_FUNCTION__: "std::string_view name()"
__PRETTY_FUNCTION__: "std::string_view name2()"
__PRETTY_FUNCTION__: "std::string_view name3()"
__PRETTY_FUNCTION__: "std::string_view name()"
----
to
----
__PRETTY_FUNCTION__: "std::string_view name()"
__PRETTY_FUNCTION__: ""
__PRETTY_FUNCTION__: ""
__PRETTY_FUNCTION__: "std::string_view name()"
----
To the best of my knowledge, there is no reason why `consteval` instead of `constexpr` should change the meaning of the functions `name2` and `name3`.
Confusingly, none of the `static_assert` that check for emptiness fail, even when the return value is in fact empty.
In case it is relevant, GCC is not affected by this issue, the behavior between `constexpr` and `consteval` is consistent
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcV11v2zYU_TX0y0UEmbRk50EPjpIMBYa2aNNhezIo6UriQpEeSdnxfv1Affgjdtps6TpgQAAp5r2H5557aF5za0WlEBMS3ZDodsJbV2uTlPgozCTTxS5JtbKiQAOlllJvhaoAn3izlkjCJQmXV1dXV_6NMqFy2RYIhKVCW2eQN4Tdna1ZZ4SqVhuB22G5iyiwFArhy-e79MP7zw93vyx_HlLLAssLnw8J6d2vHz9BrpV1uOGyX0Np8cWop7UZolQhyp5Av25dQdiSsOURR1C8QUIXhF6T-Q0JlwAABl1rFKxWHz_dPTz8trr_8j59ePfh_WpFmI8h89tX4dJT4D0_4K3TULYqd0IrIOz2Igahi0sU6HXP4hjQOu5E3uNuuGyxA-273ndlhBd_4soFQfCueLK-QXRxkb51htD0QEuoAp9WFv9oUeVIWOrzgyDoIK47Nmf67ZO5MXxH5jceNLrpdo5ufTJNgdA5idLQP-a3Y2H-lS5GgU6INPwRV2dsxtDA1zeozu48I480CtartOLWonFd1PQF3TsNCb0OsFm73QB4EP55gZeTn5mFhEuHzVpyh19vSbh85hTbZsMO3K56Mb9X28amvbFlrz0R7D8-EReV_J_5zDrT5g5qlGs0-1M56HEs0IUeHZQ6-l7cw3cv3-jw7KTDzxp8gO_Z9RB9Ha8oeX7ThQYFd3zUKu1BjxtyZkqhHDRcqBNqz5uktDsq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