Код: Выделить всё
template
struct static_string {
std::array elements_;
constexpr static_string() noexcept : elements_{} {}
constexpr static_string(const char (&data)[N + 1]) noexcept {
for (size_t i = 0; i < N + 1; i++) {
elements_[i] = data[i];
}
}
};
template
static_string(const char (&)[N]) -> static_string;
И я хочу построить из constexpr std::string_view, поэтому я добавляю:
Код: Выделить всё
constexpr static_string(std::string_view sv) noexcept {
for (size_t i = 0; i < N; ++i) {
elements_[i] = sv[i];
}
}
Код: Выделить всё
constexpr std::string_view sv = "this is a str";
constexpr static_string str2(sv);
Код: Выделить всё
: In function 'int main()':
:33:47: error: 'static_string{std::array{std::__array_traits::_Type{'t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r'}}}' is not a constant expression
33 | constexpr static_string str2(sv);
| ^
:33:47: error: 'static_string(sv)' is not a constant expression because it refers to an incompletely initialized variable
Compiler returned: 1
Подробнее здесь: https://stackoverflow.com/questions/793 ... tring-view