I have a function template that takes an arbitrarily nested list and returns an array:
#include <array>
#include <initializer_list>
template<size_t N, typename List>
std::array<size_t,N> some_function (const List& list)
{
// N is the number of times the list is nested.
std::array<size_t,N> arr;
return arr;
}
When I use this function for some nested std::initializer_list
, like this:
int main () {
using List = std::initializer_list<std::initializer_list<double>>;
List list = {{1.,2.,3.},{4.,5.,6.}};
std::array<size_t,2> arr;
arr = some_function (list);
return 0;
}
I receive the error that type N can not be deduced
couldn't deduce template parameter ‘N’
Question
- How can I improve my function template to deduce the number of times a list is nested?
- Are there better alternatives than
std::initializer_list
for this case?