Сценарий использования — синтаксический анализатор constexpr, который возвращает «сложный» объект, имеющий в себе гибкое количество элементов различных типов.
Я видел, могу ли я создать объект constexpr для std::set?, который, казалось, действовал в том же направлении, но я уже заменил std::set на Frosted::set в возвращаемом значении.
Однако каким-то образом мне нужно определить его параметр размера.
Код: Выделить всё
frozen
Поскольку std::set не имеет constexpr code> size, я попытался поместить между ними std::vector, что, видимо, не помогло.
У меня есть упрощенный код:
Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
using namespace std::string_view_literals;
constexpr auto test(const std::string_view input) {
std::set my_set;
my_set.emplace(input); // For sure the reality is more complicated and can lead to a flexible number of elements
constexpr std::vector my_vec(my_set.begin(), my_set.end());
constexpr std::array my_arr{};
std::copy_n(my_vec.begin(), my_vec.size(), my_arr.begin());
constexpr frozen::bits::carray my_carr(my_arr);
return frozen::set(my_carr);
}
int main() {
constexpr auto frozen_set = test("test"sv);
return 0;
}
Код: Выделить всё
test.cpp: In function ‘constexpr auto test(std::string_view)’:
test.cpp:14:68: error: temporary of non-literal type ‘std::set::iterator’ {aka ‘std::_Rb_tree::const_iterator’} in a constant expression
14 | constexpr std::vector my_vec(my_set.begin(), my_set.end());
| ~~~~~~~~~~~~^~
In file included from /usr/include/c++/14/set:62,
from test.cpp:3:
/usr/include/c++/14/bits/stl_tree.h:324:12: note: ‘std::_Rb_tree_const_iterator’ is not literal because:
324 | struct _Rb_tree_const_iterator
| ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:324:12: note: ‘std::_Rb_tree_const_iterator’ is not an aggregate, does not have a trivial default constructor, and has no ‘constexpr’ constructor that is not a copy or move constructor
test.cpp:16:59: error: request for member ‘begin’ in ‘my_arr’, which is of non-class type ‘const int’
16 | std::copy_n(my_vec.begin(), my_vec.size(), my_arr.begin());
| ^~~~~
Подробнее здесь: https://stackoverflow.com/questions/783 ... turn-a-std