I cannot make my head around variadic tempates. I want to do very simple thing
Tuple t{1,2,3};
should crate tuple of size 3 containing array {1,2,3} ( t.data = {1,2,3})
That means it should do 2 things:
- Create
Tuple<T,3>size 3 (Tuple<>::data[3]) - fill
Tuple<>::datawith the numbers form std::initializer_list
This does not work:
template<typename T, T...args>
struct Tuple{
T data[sizeof...(args)];
Tuple(const T& args...):data{args...}{};
};
I tried all sorts of variations like:
template<typename T, T...args>
//template<typename T, Args...args>
struct Tuple{
T data[sizeof...(args)];
//T data{args...};
//template <typename ...Args>
//Tuple(T... args):data{args...}{};
Tuple(const T& args...):data{args...}{};
//Tuple(T* ...args):data{args...}{};
};
perhaps I don't get difference between
T...args and typename ...Args and args...
I'm trying to use this as simple example to understand variadic templates and avoid using std::initializer_list
This is surprisingly difficult. The only way I could think of making this work is to have the array size as a template parameter, instead of somehow deducing this from the actual constructor parameters, and to use C++17 deduction guides.
Tested with gcc 9.1, with
-std=c++17: