Reputation: 563
Is a partial specialization allowed to have more template parameters than the primary template? My understanding was that the a partial specialization must have either lesser or the same number of template parameters as the primary template.
I am reading C++ Templates (2nd Edition) and in that it is mentioned on Section 5.4 (Page 72) that
template <typename T, std::size_t SZ>
struct MyClass<T[SZ]>{
static void print(){}
};
and
template <typename T, std::size_t SZ>
struct MyClass<T (&)[SZ]>{
static void print(){}
};
are both partial specializations of the primary template
template <typename T>
struct MyClass;
The accompanying code works fine. But is this correct? Can a partial specialization have more template parameters than the primary template?
Edit - This question has been marked a duplicate of another question but the answers there are unrelated to the question here. The Question here is reagarding the number of template parameters and the comments and quick rereading of the standard clarified the answer for me.
Upvotes: 8
Views: 1426
Reputation: 171127
Yes, a partial specialisation can indeed have more template parameters than the primary template. A typical example of this use is std::function
:
template <class T>
struct function;
template <class R, class... A>
struct function<R (A...)>
{
// std::function as we know it
};
Upvotes: 10