有人已经问过这个问题,但该线程最终以原始问题没有得到回答。
假设你有这个:
template<size_t i, class f_type>
void call_with_i(f_type f);
functor_type 是:
a) 具有以下签名的方法的结构体:
template<size_t i> operator()() const;
或者,b) 一个如下所示的函数:
template<size_t i> foo();
成员模板是一个从属名称,因为它的语义取决于成员的类型f_type
。这意味着您应该在其名称之前放置“模板”(以消除“小于”标记的使用歧义),类似于您应该放置的方式typename
在依赖限定名称之前:
template<size_t i, class f_type>
void call_with_i(f_type f) {
f.template operator()<i>();
// f.template foo<i>();
}
作为解决方法,您可以使用辅助类型:
template<size_t N> struct size_t_ { }; // or boost::mpl::int_
template<size_t i, class f_type>
void call_with_i(f_type f) {
f(size_t_<i>());
}
现在,您可以定义您的operator()
如下:
template<size_t i> void operator()(size_t_<i>) const {
// i was deduced automatically by the function argument.
}
这对于模板化构造函数来说很方便,但你不能这样做f_type()<i>()
或者其他的东西。他们会have在这种情况下可以推论。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)