I defined the following class, expecting to be able to use std::array
in a constexpr
context, but calling the data function results in a compilation error.
#include <array>
#include <cstddef>
template <std::size_t N>
struct ConstexprString
{
consteval ConstexprString(const char(&_str)[N])
: char_array(std::to_array(_str))
{
}
consteval auto Data() const -> const char*
{
return char_array.data();
}
private:
const std::array<char, N> char_array;
};
int main()
{
constexpr ConstexprString csexpr_str("123\\0");
constexpr auto d = csexpr_str.Data();
}
Report compile error on MSVC
constexpr auto d = csexpr_str.Data();
// error C2131: expression did not evaluate to a constant
csexpr_str
is a local variable with non-static duration. Make it static, and GCC is happy to use it as an argument to consteval
functions:
int main()
{
static constexpr ConstexprString csexpr_str("123\\0");
constexpr auto d = csexpr_str.Data();
}
Unless MSVC is badly broken, it should be happy too.