C++ 使用模板化arity存储在类函数中

C++ 使用模板化arity存储在类函数中,c++,function,lambda,C++,Function,Lambda,我想要一个类,它表示整数上的离散函数。函数的arity是一个模板参数。构造函数应该接受指向的指针?这个算术的功能。我还希望能够将lambda表达式传递给构造函数。事实上,这是我将要传递的主要函数类型 此外,我希望有一个方法eval来计算所提供参数的函数值 问题是如何传递和存储函数,以及如何评估它 样板 类离散函数{ 私人: FuncType f;//FuncType应该是什么? 公众: DiscreteFundType f:ff{}; int evalconst数组和x常量{ //如何传递参数以

我想要一个类,它表示整数上的离散函数。函数的arity是一个模板参数。构造函数应该接受指向的指针?这个算术的功能。我还希望能够将lambda表达式传递给构造函数。事实上,这是我将要传递的主要函数类型

此外,我希望有一个方法eval来计算所提供参数的函数值

问题是如何传递和存储函数,以及如何评估它

样板 类离散函数{ 私人: FuncType f;//FuncType应该是什么? 公众: DiscreteFundType f:ff{}; int evalconst数组和x常量{ //如何传递参数以便计算fx[0]、x[1]、。。。 } }; 您可以使用std::index_序列和一些间接方法:

template <std::size_t, typename T>
using always_t = T;

template <typename Seq> class DiscreteFunImpl;

template <std::size_t ... Is>
class DiscreteFunImpl<std::index_sequence<Is...>>
{
private:
    std::function<int (always_t<Is, int>...)> f;
public:
    DiscreteFunImpl(std::function<int (always_t<Is, int>...)> f): f(f) {}

    int eval(const array<int, sizeof...(Is)>& x) const {
        return f(x[Is]...);
    }
};


template <std::size_t N>
using DiscreteFun = DiscreteFunImpl<std::make_index_sequence<N>>;
您可以使用std::index_序列和一些间接方法:

template <std::size_t, typename T>
using always_t = T;

template <typename Seq> class DiscreteFunImpl;

template <std::size_t ... Is>
class DiscreteFunImpl<std::index_sequence<Is...>>
{
private:
    std::function<int (always_t<Is, int>...)> f;
public:
    DiscreteFunImpl(std::function<int (always_t<Is, int>...)> f): f(f) {}

    int eval(const array<int, sizeof...(Is)>& x) const {
        return f(x[Is]...);
    }
};


template <std::size_t N>
using DiscreteFun = DiscreteFunImpl<std::make_index_sequence<N>>;

您可以为f类型添加一个模板参数,然后eval就可以了。注意,std::array是一个类似元组的容器

template<int arity, typename FuncType>
class DiscreteFun {
private:
    FuncType f;
public:
    DiscreteFun(FuncType f): f(f) { };

    int eval(const array<int,arity>& x) const {
        return std::apply(f, x);
    }
};

template<int arity, typename FuncType>
DiscreteFun<arity, FuncType> makeDiscreteFun(FuncType&& f)
{
    return { std::forward<FuncType>(f) };
}

您可以为f类型添加一个模板参数,然后eval就可以了。注意,std::array是一个类似元组的容器

template<int arity, typename FuncType>
class DiscreteFun {
private:
    FuncType f;
public:
    DiscreteFun(FuncType f): f(f) { };

    int eval(const array<int,arity>& x) const {
        return std::apply(f, x);
    }
};

template<int arity, typename FuncType>
DiscreteFun<arity, FuncType> makeDiscreteFun(FuncType&& f)
{
    return { std::forward<FuncType>(f) };
}

不能将FuncType也作为模板参数传递吗?直觉上,FuncType应该由arity定义,即,如果arity=3,那么函数应该接受3个整数并返回整数。我很想将eval重命名为operator。你不能将FuncType也作为模板参数传递吗?直觉上,FuncType应该由arity定义,也就是说,如果arity=3,那么函数应该接受3个整数并返回整数。我很想将eval重命名为operator,那么我应该如何在代码中的某个地方声明一个arity为3的函数?第二个模板参数的类型应该是什么?可以推导第二个参数。如果您想进行类型擦除,请使用Jarod的答案。@Caleth,这不是一个推断的上下文CTAD是全部还是全部。您仍然可以创建makeDiscreteFun;我得到一个错误,模板参数的数目错误1,应该是2@YauhenYakimenka是的,这正是我的意思,如果你想要类型擦除,使用Jarod的答案。那么我应该如何在我的代码中声明一个arity为3的函数呢?第二个模板参数的类型应该是什么?可以推导第二个参数。如果您想进行类型擦除,请使用Jarod的答案。@Caleth,这不是一个推断的上下文CTAD是全部还是全部。您仍然可以创建makeDiscreteFun;我得到一个错误,模板参数的数目错误1,应该是2@YauhenYakimenka是的,这正是我的意思如果你想删除,使用Jarod的答案。这里的always\u t是什么意思?这是一种将is转换为t型的方法。所以在我们的例子中有N个整数。这里的always\u t是什么意思?这是一种将is转换为t型的方法。所以在我们的例子中有N个整数。