加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 可以传递函数模板作为模板参数吗?

发布时间:2020-12-16 05:57:56 所属栏目:百科 来源:网络整理
导读:我们来屁股,我们有一个模板功能: templatetypename T1,typename T2,typename T3T3 such_fun(T1 a,T2 b) { // do something...} 现在我们想用它作为另一个模板中的参数,例如像那样 templatetypename T1,templatetypename,typename,typename some_functionvoi
我们来屁股,我们有一个模板功能:
template<typename T1,typename T2,typename T3>
T3 such_fun(T1 a,T2 b) {
    // do something...
}

现在我们想用它作为另一个模板中的参数,例如像那样

template<typename T1,template<typename,typename,typename> some_function>
void big_fun(T1 a) {
   // some code...
   a = some_function<T1,T1,T1>(a,a);
   // some code...
}

可能吗?

我知道我可以使用具有define()运算符的struct.我只是好奇的功能.

编辑:

在我写这个问题的时候,我的朋友发现了一个部分解决方案:

template<typename T1,T1 (*some_function)(T1,T1)>
void big_fun(T1 a) {
   // some code...
   a = some_function(a,a);
   // some code...
}

但仍然 – 我很好奇,如果可能没有在调用之前实现函数类型.例如 – 我可能想要使用各种类型的组合来调用传递的模板:

template<typename T1,typename> some_function>
void big_fun(T1 a,T2 b) {
   // some code...
   a = some_function<T1,a);
   a = some_function<T1,T2,b);
   b = some_function<T2,T2>(b,a);
   // some code...
}

解决方法

不,这是不可能的.从N3337的14.3.3:

A template-argument for a template template-parameter shall be the
name of a class template or an alias template,expressed as
id-expression . When the template-argument names a class template,
only primary class templates are considered when matching the template
template argument with the corresponding parameter; partial
specializations are not considered even if their parameter lists match
that of the template template parameter.

第一段只明确提到类模板.我想这也不是真的值得的麻烦,因为你可以做一些与函数非常相似的东西或std ::函数作为参数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读