Template 模板方法的特化
好吧,我有:
template<typename T>
class Reader
{
class Input
{
template<typename C>
void operator()(C& val) const
{
/* Do Some Work */
}
};
};
不幸的是,“做一些工作”的通用版本对我不起作用。它也不容易修改,因为它位于一些繁重的模板元编程代码的中间。
所以我想我可以专门针对我的类型使用该方法。所以我的第一步是尝试将通用方法从课堂中拉出来。
template<typename T>
class Reader
{
class Input
{
template<typename C>
void operator()(C& val) const;
};
};
template<typename T>
template<typename C>
void typename Reader<T>::Input template operator()<C>(C& val) const // LINE 13
{
/* Do Some Work */
}
不幸的是我收到错误:
sh:13: 错误:错误:在“&”之前应有“)”令牌
OK I have:
template<typename T>
class Reader
{
class Input
{
template<typename C>
void operator()(C& val) const
{
/* Do Some Work */
}
};
};
Unfortunately the generic version of "Do Some Work" does not work for me. Nor it is easy to modify because it is in the middle of some heavy template meta programming code.
So I though I could specialize the method for my type. So my first step was to try and pull the general method out of the class.
template<typename T>
class Reader
{
class Input
{
template<typename C>
void operator()(C& val) const;
};
};
template<typename T>
template<typename C>
void typename Reader<T>::Input template operator()<C>(C& val) const // LINE 13
{
/* Do Some Work */
}
Unfortunately I am getting the error:
s.h:13: error: error: expected ‘)’ before ‘&’ token
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是像平常一样编写它
在类之外定义通用版本并不能帮助您提供它的特殊版本,或者我确实错过了您在这里的目标。
Just write it like normal
Defining the generic version out of class doesn't help you with providing special versions of it though, or I do miss the goal of yours here.
我想这是不可能的,因为它算作部分函数模板专业化,这是不允许的。
void Reader::Input::operator () (C& int)
有一个Reader类型的隐式第一个参数(
,因此其签名实际上是this
指针) ;::Input *void (Reader::Input *, C &)
。您正在尝试指定C
,但不是T
。I guess it is impossible as it counts as partial function template specialization, which is not allowed.
void Reader<T>::Input::operator () (C& int)
has an implicit first argument (thethis
pointer) of typeReader<T>::Input *
, hence its signature is in factvoid (Reader<T>::Input *, C &)
. You are trying to specifyC
, yet notT
.