0

I want to write a template class to calculate the size of tuple,but error occur when compiling.

template <typename... Types>
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};

enter image description here

Canny
  • 11
  • 1
  • 2

2 Answers2

1

This looks like a partial specialization without preceding primary template, which would look like template <typename> struct tuple_size_ {};.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 1
    yes,you are right. Partial or full specialization need a primary template. Thanks a lot. – Canny May 27 '22 at 09:15
0

The problem is that you're providing a partial template specialization but haven't declared the corresponding primary template. That is, there is no class template that you want to specialize in the first place. In other words, it doesn't make sense to specialize something(the class template in your case) that isn't there in the first place.

To solve this you need to provide a declaration for the primary class template:

//primary template 
template<typename> struct tuple_size_; 

//now you can partially specialize the above primary template
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};
Jason
  • 36,170
  • 5
  • 26
  • 60
  • Thinks a lot. your answer is very helpful. – Canny May 27 '22 at 09:17
  • @Can You're welcome. There are some [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that you can refer to in case you need more information about the topics described above. – Jason May 27 '22 at 09:23
  • Thanks for your recommendation. I will buy some of these books on the list. – Canny May 27 '22 at 10:09
  • @Canny Btw, most of these books are also available in pdf forms(which i prefer since that way they are more portable) and easier to use. You can choose according to your preference. – Jason May 27 '22 at 10:12