2

This answer provides a C++11 solution for the problem of overloading a template class based on the number of template parameters.

However, this approach seems to work only if the new template parameters are types, meaning this

template <typename ... Ts> struct awesome;

template <typename T, typename U> struct awesome<T, U> {};
template <typename T, unsigned int I, typename U> struct awesome<T, I, U> {};

int main() {
    awesome<int,float> aw;
    awesome<int,42,float> aw2;
}

does not compile. Can the desired behaviour achieved using a different approach?

Community
  • 1
  • 1
elemakil
  • 3,681
  • 28
  • 53
  • 5
    Could you use `std::integral_constant` instead of `42` to have a type? – Jarod42 Jun 12 '14 at 09:22
  • @Jarod42 In principle yes and I've thought about something similar. However, the way I see it, this is somewhat inconvenient and annoying to use when compared to the plain number literal. – elemakil Jun 12 '14 at 09:43
  • 5
    Your code trying to lie to the compiler. The primary template *declares* that it can accept variable number of **type** template arguments, so you've to stick to *that* declaration. If you lie, the compiler will bite you. – Nawaz Jun 12 '14 at 10:08
  • What is "the desired behaviour"? Explain what you want to achieve. – D Drmmr Jun 12 '14 at 12:03
  • My intended use-case is an `insert` function for different compile-time type containers. For the `type_list` container, the `insert` struct requires three parameters: The `type_list` (`T`), the index (`I`) where to insert the new type `U`. For the `type_map` container on the other hand, only the `type_map` container and the type to be inserted `U` are required. At the moment the functionality is achieved using two functions `insert_tlist` and `insert_tmap`, so I came upon this problem while trying to unify the interface of the type containers. – elemakil Jun 12 '14 at 12:32

0 Answers0