I have a static inline
member of a class - an std::vector
. I need to fill it with numbers - 0 through to std::numeric_limits<uint16_t>::max() - 1
.
I know there is list-initialization for containers, but I don't feel like coming up with a macro/struct that will expand to 1,2,3, all the way to 65534.
I want to know if there is a way to cleanly initialize the container with such a sequence of numbers, considering it's a static inline
variable.
The container needs to be static
, but if there is a way to do what I want that requires it to not be inline
, then so be it.
One way is to write a function that will fill the container and then use #pragma startup myfunc
or [[gnu::constructor]]
, but neither the macro nor the attribute are in the actual standard. And I don't want to have functions like init_mylibrary()
akin to glfwInit()
that the user has to call in main()
before using mylibrary
.
An approach that will possibly work is to declare the container's size through the constructor and supply a custom allocator that will initialize the memory with consecutive integers, but writing an entire allocator for this task seems like overkill. There has to be a way to do this cleanly, like how Ruby allows one to write Array.new(4) { |i| i + 10 } #=> [10, 11, 12, 13]