Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the base case with no template arguments for variadic tuple is not working?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading