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

我们来屁股,我们有一个模板功能
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 ::函数作为参数.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...