3

I define a variadic struct like so

template <class T, class... TRest>
struct Opa
{
    Opa()
    {
        std::cout << "Mutiple-arguments template";
    }
};

and want to specialize it for the case with 1 argument only as follows

template <>
struct Opa<class T>
{
    Opa()
    {
        std::cout << "One-argument template";
    }
};

but compiler just ignores this second struct, and the output from

Opa<int> opa;
Opa<int, int> opa_opa;

is Mutiple-arguments template, Mutiple-arguments template.

Specifying one-argument template in different ways, e.g.

template <class T>
struct Opa
{...}

resulted in a compilation error. I realize that my question is quite simple, but googling didn't help. So please don't throw rotten tomatoes at me, and thanks for the answers.

Alex B.
  • 348
  • 2
  • 12

1 Answers1

7

Your syntax for the single-argument specialisation is wrong. You're probably fully specialising it for an on-the-spot declared class T. You wanted this:

template <class T>
struct Opa<T>
{
    Opa()
    {
        std::cout << "One-argument template";
    }
};

Live example

Partial specialisations are declared by listing the parameters of the partial specialisation in the angle brackets after template (in your case, a single type parameter, class T), and listing the arguments for the primary template in the angle brackets after the primary template's name (in your case, a single type argument, T).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455