How to templatize partial custom templates?

I don’t even know what title to ask this question; hope the code demonstrates 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'
closing class templates are not explicitly specialized

(Turning to // Line A.) What is the correct syntax to do what I want?

+3
source share
2 answers

Just replace

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

with

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

i.e. delete template<>here.

+4
source

, . . :

#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;                                                                                                                                                                           
};                                                                                                                                                                                                                         
+1

Source: https://habr.com/ru/post/1745502/


All Articles