Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specialize template class without parameters?

Tags:

c++

templates

I have such code, but compiler says about error (error C2913: explicit specialization; 'Vector' is not a specialization of a class template d:\test_folder\consoleapplication1\consoleapplication1\consoleapplication1.cpp 28 1 ConsoleApplication1 ):

#include <iostream>

template <int N, int ... T>
class Vector
{
public:
    static void print_arguments(void)
    {
        std::cout << N << " : " << std::endl;
        Vector<T>::print_argumetns();
    }
protected:
private:
};

template <>
class Vector<>
{
public:
    static void print_arguments(void)
    {
    }
protected:
private:
};

int main(void)
{
   std::cout << "Hello world" << std::endl;
   int i = 0;
   std::cin >> i;
   return 0;
}
like image 893
LmTinyToon Avatar asked Sep 10 '26 09:09

LmTinyToon


1 Answers

You can't create a specialization of Vector with no template parameters, because Vector requires at least one.

What you can do instead is declare the primary template to take any number of template arguments, then define both cases as specializations:

//primary template
template <int... Ns>
class Vector;

//this is now a specialization
template <int N, int ... T>
class Vector<N,T...>
{
    //...
};

template <>
class Vector<>
{
    //...
};
like image 142
TartanLlama Avatar answered Sep 12 '26 01:09

TartanLlama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!