如何模板化部分模板专业化?

发布于 2024-09-01 13:30:32 字数 982 浏览 6 评论 0原文

我什至不知道该给这个问题起什么标题;希望代码能够演示我想要做的事情:

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

当 A 是任何类型 Bravo 时,我想为 Alpha 编写一个专门化,但是编译器说

“>”之前的显式专业化无效令牌
封闭的类模板没有明确专门化

(请参阅 // Line A。)执行我想要的操作的正确语法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2024-09-08 13:30:32

只需替换

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

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

即删除此处的 template 即可。

Just replace

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

with

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

i.e. remove template<> here.

何其悲哀 2024-09-08 13:30:32

您的问题似乎是重复的模板规范。您只需使用一个。像这样:

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

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;                                                                                                                                                                           
};                                                                                                                                                                                                                         
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文