Как создать constexpr static_string из constexpr std::string_viewC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Как создать constexpr static_string из constexpr std::string_view

Сообщение Anonymous »

У меня есть шаблон static_string

Код: Выделить всё

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 static_string str1("this is a str");
И я хочу построить из 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
Так почему же это выражение не получило константу, можно ли построить его из constexpr std::string_view? спасибо, см. https://www.godbolt.org/z/6TToEPnch

Подробнее здесь: https://stackoverflow.com/questions/793 ... tring-view
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»