Question

I have a class defined similarly to the following:

template< typename A, typename... B >
struct S {
    //...//
};

But I would like to create an overload (effectively) like so:

template<>
struct S<> {
    //...//
};

However the above is apparently illegal, I can not use a varidic template, and pass zero arguments to it because that could be ambiguous with the first definition (and bad practice). Is there a way to create a template specifier with zero parameters in C++?

Était-ce utile?

La solution

The problem is that you are trying to create a partial specialization that does not match the main template (the main template requires at least one argument). If you want to use a variadic template, you can instead do:

template <typename... A>
struct S {
  // One-or-more case
};

template <>
struct S<> {
  // Zero case
};

Based on comments, the poster wanted to find the first argument for the non-empty case; the modification for that is:

template <typename... A>
struct S;

template <>
struct S<> {
  // Zero case
};

template <typename A, typename... B>
struct S<A, B...> {
  // One-or-more case
};
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top