views:

62

answers:

2

I'm not even sure what title to give this question; hopefully the code will demonstrate what I'm trying to do:

#include <string>
#include <list>
using namespace std;

template<typename A> class Alpha { public: A m_alpha_a; };
template<typename B> class Bravo { public: B m_bravo_b; };

template<> class Alpha<string> { public: string m_alpha_string; };

template<typename B>
template<> class Alpha<Bravo<B> > 
{ 
public: 
    Bravo<B> m_bravo_class;   // Line A
}; 

int main()
{
    Alpha<int> alpha_int;
    alpha_int.m_alpha_a= 4;

    Alpha<string> alpha_string;
    alpha_string.m_alpha_string = "hi";

    Alpha<Bravo<int> > alpha_bravo_int;
    alpha_bravo_int.m_bravo_class.m_bravo_b = 9;
};

I want to write a specialization for Alpha<A> when A is of any type Bravo<B>, but the compiler says

invalid explicit specialization before ‘>’ token
enclosing class templates are not explicitly specialized

(Referring to // Line A.) What's the correct syntax to do what I want?

+4  A: 

Just replace

template<typename B>
template<> class Alpha<Bravo<B> > 

with

template<typename B>
class Alpha<Bravo<B> > 

i.e. remove template<> here.

doublep
So in other words, this is not a template specialization but a separate template, right?
Péter Török
Well, I'm not sure about correct terminology. When specializing, you leave any variable parameters in `template <...>` (these may be none, i.e. brackets become empty, or there may be some left), and also add (possibly still variable) specializer after class/function, as in `Alplha<Bravo<B> >`.
doublep
@Peter, all partial specializations are separate templates. But they all partially specialize a primary template.
Johannes Schaub - litb
@Johannes, hmmmm... let me put it differently then. Would the above template be compilable in itself, without the first template `Alpha` as defined by the OP?
Péter Török
@Péter no it wouldn't if it isn't declared. It's a partial specialization of some template, so it needs that template to be declared (not necessarily defined though).
Johannes Schaub - litb
@Johannes, ahaaa... now I get it, I failed to notice the syntactic difference which does indeed mark that this is a specialization. Thanks for your patience :-)
Péter Török
+1  A: 

Your problem seems to be the repeated template specifications. You need to use only one. Like this:

#include <string>                                                                                                                                                                                                          
#include <list>                                                                                                                                                                                                            
using namespace std;                                                                                                                                                                                                       

template<typename A>                                                                                                                                                                                                       
class Alpha { public: A m_alpha_a; };                                                                                                                                                                                      

template<typename B>                                                                                                                                                                                                       
class Bravo { public: B m_bravo_b; };                                                                                                                                                                                      

template<> class Alpha<string> { public: string m_alpha_string; };                                                                                                                                                         

template<typename B>                                                                                                                                                                                                       
class Alpha< Bravo<B> >                                                                                                                                                                                                    
{                                                                                                                                                                                                                          
public:                                                                                                                                                                                                                    
    Bravo<B> m_bravo_class;   // Line A                                                                                                                                                                                    
};                                                                                                                                                                                                                         

int main()                                                                                                                                                                                                                 
{                                                                                                                                                                                                                          
    Alpha<int> alpha_int;                                                                                                                                                                                                  
    alpha_int.m_alpha_a= 4;                                                                                                                                                                                                

    Alpha<string> alpha_string;                                                                                                                                                                                            
    alpha_string.m_alpha_string = "hi";                                                                                                                                                                                    

    Alpha<Bravo<int> > alpha_bravo_int;                                                                                                                                                                                    
    alpha_bravo_int.m_bravo_class.m_bravo_b = 9;                                                                                                                                                                           
};                                                                                                                                                                                                                         
krousey