As an exercise, I'm trying to define a variadic template for a Tuple but I found that the base case with no elements is not working.
template <typename Head, typename... Tail>
struct Tuple : Tuple<Tail...>
{
Tuple(const Head& head, const Tail&... tail)
: Base{tail...}, m_head{head} {}
private:
using Base = Tuple<Tail...>;
Head m_head;
};
template <> struct Tuple<> {};
MSVC 2022 gives the following error:
C:\projects\cpp\cpp_programming_language\28_metaprogramming\variadic_tuple.cpp(12): error C2976: 'Tuple': too few template arguments
C:\projects\cpp\cpp_programming_language\28_metaprogramming\variadic_tuple.cpp(2): note: see declaration of 'Tuple'
C:\projects\cpp\cpp_programming_language\28_metaprogramming\variadic_tuple.cpp(12): error C2913: explicit specialization; 'Tuple' is not a specialization of a class template
Why this does not work and how to fix it?
CodePudding user response:
One correct incantation would be
template <typename...> struct Tuple;
template <typename Head, typename... Tail>
struct Tuple<Head, Tail...> : Tuple<Tail...>
{
(the rest is identical to your code).