Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template types nested in a template class definition

I've a situation where there is a class definition that looks like this:

template<class T>
class Alpha< Bravo<T> >
{
....
};

I'm compiling with gnu g++ and the compiler is complaining that Alpha is, "is not a template".

I've seen this same technique used in the library that Bravo was written in and Bravo is a templated class. Am I missing something? I've stripped Alpha down to the bone and done testing with no compilation success. I've also tried to copy+paste code from the place I saw this working in Bravo's library and same thing, no success, same error.

Thanks in advance.

like image 394
ct_ Avatar asked Dec 29 '25 02:12

ct_


2 Answers

What you have there is a partial template specialization; however, any template specialization needs a primary template to specialize, and that you do not have.

template<class T>
class Alpha;

template<class T>
class Alpha<Bravo<T> >
{
    // ...
};
like image 138
ildjarn Avatar answered Dec 31 '25 16:12

ildjarn


You need to declare a primary template first. What you've written is a specialization.

//primary template - the definition is optional
template<class T>
class Alpha
{
};

//specialization
template<class T>
class Alpha< Bravo<T> >
{
};
like image 27
Nawaz Avatar answered Dec 31 '25 18:12

Nawaz



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!