Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variadic struct specification

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.

like image 383
Alex B. Avatar asked Aug 07 '26 22:08

Alex B.


1 Answers

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).

like image 198
Angew is no longer proud of SO Avatar answered Aug 10 '26 11:08

Angew is no longer proud of SO