Search code examples
c++template-specializationnested-class

Nested class template full specialization versus partial specialization


The following code is giving me the compile error:

error: explicit specialization in non-namespace scope 'struct Apply' template < >

            ^
#include <iostream>

struct Apply
{
    template < typename ...BaseClasses>
    struct Inheritance;

    template < typename FirstBaseClass, typename ...OtherBaseClasses>
    struct Inheritance< FirstBaseClass, OtherBaseClasses... >   : FirstBaseClass
            , Inheritance< OtherBaseClasses... >
    {

    };

    template < >
    struct Inheritance< >
    {

    };
};

struct A{ int a; };
struct B{ int b; };
struct C{ int c; };

struct Example : Apply::Inheritance< A, B, C >
{
    void print() const
    {
        std::cout << a << std::endl;
        std::cout << b << std::endl;
        std::cout << c << std::endl;
    }
};

int main()
{
    Example ex;

    ex.print();

    return 0;
}

In another post I read that the problem is the just the full template specialization, and that with a partial template specialization I would be able to fix this. But how can I change the inheritance recursion in my code to achieve this? I tried but I only broke it badly...


Solution

  • This is an XY problem. You have to simply move it outwards.

    template < >
    struct Apply::Inheritance< >
    {
    
    };