I was playing around with variadic templates and decided to implement my own tuple class. But when I tried to implement the constructors, I discovered that the implicitly created constructors seem to cover what i was about to build. Consider to following code.
template<typename ... Tail> struct mytuple { };
template<typename Head, typename ... Tail>
struct mytuple<Head, Tail...>
{
Head head;
mytuple<Tail...> tail;
};
int main()
{
mytuple<int, int, int> t1{1,2,3}; // <--
mytuple<int, int, int> t2{1,{2,{3,{}}}};
}
- Why does the the simple brace-initialization
t1{1,2,3}
already work? - Which constructor is called here?
- Why does the second initialization
t2{1,{2,{3,{}}}}
work as well and not only one and not the other?
My guess is that this "magic" is done by some implicitly defined initializer-list constructor (since C++11 I'm not sure which constructors are defined under which conditions). In case of the second tuple t2{1,{2,{3,{}}}}
I kind of understand why this is working, since the structure of the braces is reflecting the structure of the constructed class. But to my surprise both variants seem to work.