Диапазоны constexpr дают другой результат, чем не constexprC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Диапазоны constexpr дают другой результат, чем не constexpr

Сообщение Anonymous »


I have the following code:

#include #include auto func(const char* Str) { return std::string_view(Str) | std::views::split(',') | std::views::transform([](auto inp) { return inp.data(); }) | std::ranges::to(); } Note I know that the result of func(test), where test = "a,b" would be to pointers pointing into testand thus printing them would result in["a,b", "b"]` but I am only concerned with the beginning of the string anyways.

I tried to make this function consteval, as i saw that all the std::ranges functions are allconstexpr. This was my attempt:

template consteval auto func() { return std::string_view(Str) | std::views::split(',') | std::views::transform([](auto inp) { return inp.data(); }) | std::ranges::to(); } But I get the error (See demo)

error: call to consteval function 'func' is not a constant expression
note: pointer to subobject of heap-allocated object is not a constant expression

where test is a "string" of type static constexpr char[]. I know that this error stems from std::vector allocating memory, but I thought std::vector supports constexpr. Though maybe it is the problem of std::ranges::to which does not treat the destination container type as constexpr.

I have also tried to use std::ranges::to but to did not support that either. Is there another container which supports random access and constexpr which works with std::ranges::to?


Источник: https://stackoverflow.com/questions/781 ... -constexpr
Реклама
Ответить Пред. темаСлед. тема

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

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

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

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

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

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