Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ variadic templates. Simple Sample code won't compile, complains 'Not a template'

I'm poking around this unfamiliar territory, and thought I'd try a simple example from Danny Kalev's tutorial on the matter. The code is pretty simple:

template<> struct Count<> { static const int value = 0;};

template<typename T, typename... Args>
    struct Count<T, Args...> //partial specialization
{ 
    static const int value = 1 + Count<Args...>::value;
};

But gcc 4.4.7 and even 4.7.0 complain (despite -std=c++0x -std=gnu++0x flags) that:

/src/tests/VTemplates.h:12:8: error: 'Count' is not a template
/src/tests/VTemplates.h:12:18: error: explicit specialization of non-template 'Count'
/src/tests/VTemplates.h:16:8: error: 'Count' is not a template
/src/tests/VTemplates.h:16:26: error: 'Count' is not a template type

what am I missing?

like image 706
Arunas Avatar asked Dec 28 '22 02:12

Arunas


1 Answers

template<> struct Count<> { static const int value = 0;};

is a template specialization with no template arguments. But you can't specialize a template class that is not already a non-specialized template. In other words you must first establish a non-specialized version of the template class, and only then can you specialize it. So for instance, if you did:

//non-specialized template
template<typename... Args>
struct Count;

//specialized version with no arguments
template<> struct Count<> { static const int value = 0;};

//another partial specialization
template<typename T, typename... Args>
struct Count<T, Args...> 
{ 
    static const int value = 1 + Count<Args...>::value;
};

That would work.

like image 164
Jason Avatar answered Dec 29 '22 15:12

Jason