Диапазоны constexpr дают другой результат, чем не constexpr ⇐ C++
Диапазоны constexpr дают другой результат, чем не constexpr
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
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Ошибка! Переменная constexpr должна быть инициализирована константным выражением constexpr
Anonymous » » в форуме C++ - 0 Ответы
- 50 Просмотры
-
Последнее сообщение Anonymous
-