0

First of all I'm very sorry for the question title, but it's very hard to describe. What of those two below is valid syntax if I want to specialized Resolve for all instantiation of A?
1)

template<uint32_t I> struct A {};

template<typename> struct Resolve;

template<uint32_t I>
struct Resolve<A<I>>
{
    void f() { printf("im here!\n"); }  
};

2)

template<uint32_t I> struct A {};

template<typename> struct Resolve;

template<>
template<uint32_t I>
struct Resolve<A<I>>
{
    void f() { printf("im here!\n"); }  
};

Or is template<> optional? There's two different answers on SO: here and here.
Also please provide quotation of the standard if possible.

Option 2) doesn't compile on MSVC, but does compile at least on some versions of GCC.

Criss
  • 581
  • 5
  • 17
  • The accepted answer of the second post clearly states that the `template <>` bit is not valid. – R Sahu Aug 23 '18 at 17:35
  • But unfortunately doesn't say why – Criss Aug 23 '18 at 17:36
  • 1
    I am not sure what you are looking for. If there isn't anything in the standard that says it is valid, then it is invalid. – R Sahu Aug 23 '18 at 17:39
  • Well, maybe standard says somewhere about this sort of cases. If not, then I'll go with the answet that the first one is not valid – Criss Aug 23 '18 at 17:42

1 Answers1

1

This is correct:

template <uint32_t I>
struct Resolve<A<I>>
{ };

The syntax template <> is used to introduce an explicit specialization (of a class template, function template, whatever) (see [temp.spec]/3 and [temp.expl.spec]/1). But we're trying to do a partial specialization. A partial specialization still needs to introduce template parameters, an explicit specialization does not.

On the other hand, if we were trying to specialize a member of an explicit specialization, then we'd use template <>. For instance:

template <class T>
struct A {
    template <class T2> struct B { }; // #1
};

template <>         // for A
template <class T2> // for B
struct A<int>::B<T2> { }; // #2

A<char>::B<int> x; // #1
A<int>::B<char> y; // #2
Barry
  • 286,269
  • 29
  • 621
  • 977