【问题标题】:Template class with function pointer parameter带有函数指针参数的模板类
【发布时间】:2015-01-20 05:33:46
【问题描述】:

有没有办法重写curry 模板类定义使main 接受curry<addtogether> 而不是当前的curry<int,int,int,addtogether>

#include<iostream>

int addtogether(int x,int y){
    return(x+y);};

template<class T,class U,class V,T(*F)(U,V)>
class curry{
private:
    const U y;
public:
    curry(U x):y(x){};
    T operator()(V v){return(F(y,v));};
};

int main(){
    {using namespace std;
    cout<<curry<int,int,int,addtogether>(1)(1);}
};

这应该是可行的,因为addtogether 在编译时是已知的。我只是没有看到很多带有函数指针的模板。大多数是int(*f)(int,int) 的形式,它的多态性不够。我正在寻找一个模板定义,它可以接受任何带有两个参数的函数指针。

谢谢!

编辑:如果我的要求确实是不可能的,我想到了以下解决方法:

#include<iostream>

class addtogether{
public:
    typedef int v1;
    typedef int v2;
    typedef int v0;
    int operator()(int x,int y){
    return(x+y);};
};

template<class F>
class curry{
public:
    typedef typename F::v2 v1;
    typedef typename F::v0 v0;
    const typename F::v1 y; 
    curry(const typename F::v1 x):y(x){};
    v0 operator()(const v1 v){return(F()(y,v));};
};

int main(){
{using namespace std;
    cout<<curry<addtogether>(1)(1);}
};

我什至可以考虑用类型列表替换类型占位符 v0 v1 v2。 无论如何我都想分享一些东西......

【问题讨论】:

  • 不作为类(类型被推导的模板非类型参数相对经常被请求)。但是您可以改用函数模板,并将函数指针作为运行时参数传递(依靠编译器优化来删除该间接)或将其包装在 lambda 中。
  • 我不敢相信这是不可能的!什么极端情况会阻止编译器在编译时推断函数指针的类型?
  • 如果operator()没有重载,你可以从operator()的签名中推断出vX的类型。此外,您没有必须推断这些类型 - 您只需要存储参数,然后将它们传递给某个函数(对象)。见第一个链接:How to make curry?

标签: c++ templates function-pointers currying


【解决方案1】:

有没有办法重写curry 模板类定义使main 接受curry&lt;addtogether&gt; 而不是当前的curry&lt;int,int,int,addtogether&gt;

不可以,因为非类型模板参数F依赖于前面的模板参数,所以不能在它们之前声明。

你真的需要函数指针成为类型的一部分,而不是存储为curry 的成员变量吗?使用成员变量将允许在函数模板中推导它:

template<class T,class U,class V>
class curry{
private:
    T (*f)(U,V);
    const U y;
public:
    curry(T (*f)(U,V), U x): f(f), y(x){};
    T operator()(V v){return(f(y,v));};
};

template<class T,class U,class V>
curry<T, U, V>
make_curry(T (*f)(U,V), U u)
{
  return curry<T, U, V>(f, u);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2014-04-14
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多