sorte33
sorte33

Reputation: 35

C++ partial template template specialization

I want to pass a partial template specialization to template template parameter but I'm getting an error. Im not sure why excatly this doesent work.

template<template<typename, int> class V, typename T, int N, int... Indexes>
class Swizzle
{
    // ...
};

template<typename T, int N>
struct Vector;

template<typename T>
struct Vector<T, 3>
{
    // ...

    union
    {
        // ...
        Swizzle<Vector, T, 3, 0, 0, 0> xxx;
    };
};

Error:

'Vector': invalid template argument for template parameter 'V', expected a class template 'Swizzle': use of class template requires template argument list

Problem appears only on MSVC

Upvotes: 3

Views: 215

Answers (2)

Jarod42
Jarod42

Reputation: 218323

Within the class with class name injection, Vector might refer to both the type of this instance of the template and the template itself.

In the following cases, the injected-class-name is treated as a template-name of the class template itself:

[..]

  • it is used as a template argument that corresponds to a template template parameter

So Msvc is incorrect here.

Possible workaround:

Swizzle<::Vector, T, 3, 0, 0, 0> xxx;

Demo

Upvotes: 1

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275956

Within the class template Vector, Vector refers to both the type of this instance of the template and the template itself.

This should compile:

template<class X, int M>
using Self = Vector<X,M>;
// ...

union
{
    // ...
    Swizzle<Self, T, 3, 0, 0, 0> xxx;
};

I suspect MSVC is wrong here, but am uncertain.

Upvotes: 3

Related Questions