2

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; } 
5
  • without parameters it isnt a specialization, I would rather call it a "generalization" which isnt possible afaik Commented Mar 31, 2016 at 9:49
  • In other words, I cannot create template without any parameters? Commented Mar 31, 2016 at 9:51
  • 1
    I dont understand, why you want to do this? How is this supposed to be used? Commented Mar 31, 2016 at 9:52
  • For educational purposes, I just wanted to create class, which will print own arguments in sorted order Commented Mar 31, 2016 at 9:53
  • 1
    turns out, that I was wrong, see Tartans answer... Commented Mar 31, 2016 at 9:53

2 Answers 2

9

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<> { //... }; 
Sign up to request clarification or add additional context in comments.

5 Comments

Great! thanks, but I cannot understand, why this version of code works. Can you give me some links to explanations, please?
What do you not understand in particular?
Why primary template declaration is needed?
Because if you don't have one, Vector is a class template which needs at least one template argument. With the primary template, Vector is a class template which can have any number of template arguments and there are specializations for when there are none and when there is at least one.
Thanks, I guess that I understood
0

What you probably want is this:

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

The second is a "terminating" partial specialization, which is used when only one parameter is used.

2 Comments

This version will not work in such case, I guess : Vector<> vector;
Yes, the TartanLlama version is better as it works even in that case, the general idea is the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.