Search code examples
c++templatespartial-specializationtemplate-templates

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


Solution

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