如何模板化部分模板专业化?
我什至不知道该给这个问题起什么标题;希望代码能够演示我想要做的事情:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需替换
为
即删除此处的
template
即可。Just replace
with
i.e. remove
template<>
here.您的问题似乎是重复的模板规范。您只需使用一个。像这样:
Your problem seems to be the repeated template specifications. You need to use only one. Like this: