Saving the number of arguments to the variational pattern template

In the following example

template <size_t... Entries> struct StaticArray { enum {N = sizeof...(Entries)}; size_t array[N] = {Entries...}; }; 

saving the number of entries in enum seems more like a hack.

Is this really a tutorial way to store the number of records or is there a cleaner way to do this?

+5
source share
1 answer

I would prefer a static constexpr member:

 template <size_t... Entries> struct StaticArray { static constexpr size_t N = sizeof...(Entries); size_t array[N] = {Entries...}; }; 

Perhaps with a number of other member functions constexpr ( size() , begin() , end() , data() , etc.)

+6
source

Source: https://habr.com/ru/post/1247383/


All Articles