简体   繁体   English

使用模板元编程在C ++中实现“深层”功能

[英]“Deep” function currying in C++ using template metaprogramming

I just came up with an (yet another!) implementation of function currying in C++ using template metaprogramming. 我只是想出了使用模板元编程在C ++中实现函数泛滥的(还有另一个!)实现。 (I am almost sure other implementations are better / more complete than mine, but I am doing this for learning purposes, a case in which I think reinventing the wheel is justified.) (我几乎可以肯定,其他实现比我的实现更好/更完善,但是出于学习目的,我这样做是出于这种考虑,我认为重新发明轮子是合理的。)

My funcion currying implementation, test case included, is the following one: 我的funcion currying实现(包括测试用例)如下:

#include <iostream>
#include <functional>

template <typename> class curry;

template <typename _Res>
class curry< _Res() >
{
  public:
    typedef std::function< _Res() > _Fun;
    typedef _Res _Ret;

  private:
    _Fun _fun;

  public:
    explicit curry (_Fun fun)
    : _fun(fun) { }

    operator _Ret ()
    { return _fun(); }
};

template <typename _Res, typename _Arg, typename... _Args>
class curry< _Res(_Arg, _Args...) >
{
  public:
    typedef std::function< _Res(_Arg, _Args...) > _Fun;
    typedef curry< _Res(_Args...) > _Ret;

  private:
    class apply
    {
      private:
        _Fun _fun;
        _Arg _arg;

      public:
        apply (_Fun fun, _Arg arg) 
        : _fun(fun), _arg(arg) { }

        _Res operator() (_Args... args)
        { return _fun(_arg, args...); }
    };

  private:
    _Fun _fun;

  public:
    explicit curry (_Fun fun)
    : _fun(fun) { }

    _Ret operator() (_Arg arg)
    { return _Ret(apply(_fun, arg)); }
};

int main ()
{
  auto plus_xy = curry<int(int,int)>(std::plus<int>());
  auto plus_2x = plus_xy(2);
  auto plus_24 = plus_2x(4);
  std::cout << plus_24 << std::endl;

  return 0;
}

This function currying implementation is "shallow", in the following sense: If the original std::function 's signature is... 在以下意义上,此函数周期性实现为“浅”:如果原始std::function的签名为...

(arg1, arg2, arg3...) -> res

Then the curried function's signature is... 那么curried函数的签名是...

arg1 -> arg2 -> arg3 -> ... -> res

However, if any of the arguments or the return type themselves can be curried, they do not get curried. 但是,如果任何参数或返回类型本身可以被管理,则它们不会被管理。 For example, if the original std::function 's signature is... 例如,如果原始std::function的签名是...

(((arg1, arg2) -> tmp), arg3) -> res

Then the curried function's signature will be... 然后咖喱函数的签名将是...

((arg1, arg2) -> tmp) -> arg3 -> res

Instead of... 代替...

(arg1 -> arg2 -> tmp) -> arg3 -> res

Which is what I want. 这就是我想要的。 So I would like to have a "deep" currying implementation. 因此,我希望有一个“深层”的可实现的实现。 Does anyone know how I could write it? 有人知道我怎么写吗?


@vhallac: @vhallac:

This is the kind of function that should be passed to the constructor of curry<int(int(int,int),int)> : 这是应该传递给curry<int(int(int,int),int)>的构造函数的函数:

int test(std::function<int(int,int)> f, int x)
{ return f(3, 4) * x; }

Then one should be able to do the following: 然后,一个人应该能够执行以下操作:

auto func_xy = curry<int(int(int,int),int)>(test);
auto plus_xy = curry<int(int,int)>(std::plus<int>());
auto func_px = func_xy(plus_xy);
auto func_p5 = func_px(5);
std::cout << func_p5 << std::endl;

I have implemented a cheating version of a decurry class to demonstrate how you would go about implementing the specialization. 我已经实现了一个decurry类的作弊版本,以演示如何实现专业化。 The version is cheating, because it gets declared as a friend of curry<T> , and accesses the internal _fun to convert a curried version of a function back to original. 该版本是作弊的,因为它被声明为curry<T>的朋友,并访问内部_fun将函数的咖喱版本转换回原始版本。 It should be possible to write a generic one, but I didn't want to spend more time on it. 应该可以编写一个通用的,但是我不想花更多的时间在上面。

The decurry implementation is: decurry实现为:

template <typename _Res, typename... _Args>
class decurry< curry<_Res(_Args...)> > {
public:
    typedef curry<_Res(_Args...)> _Curried;
    typedef typename curry<_Res(_Args...)>::_Fun _Raw;

    decurry(_Curried fn): _fn(fn) {}

    _Res operator() (_Args... rest) {
        return _fn._fun(rest...);
    }
private:
    _Curried _fn;
};

And it requires the line: 它需要以下行:

friend class decurry< curry<_Res(_Arg, _Args...)> >;

inside class curry< _Res(_Arg, _Args...) > to give our class access to curry<T>._fun . class curry< _Res(_Arg, _Args...) >以使我们的类可以访问curry<T>._fun

Now, the specialization can be written as: 现在,专业化可以写成:

template <typename _Res, typename _Res2, typename... _Args2, typename... _Args>
class curry< _Res(_Res2(_Args2...), _Args...) >
{
public:
    typedef curry< _Res2(_Args2...) > _Arg;
    typedef std::function< _Res2(_Args2...) > _RawFun;
    typedef std::function< _Res(_RawFun, _Args...) > _Fun;
    typedef curry< _Res(_Args...) > _Ret;

private:
    class apply
    {
    private:
        _Fun _fun;
        _RawFun _arg;

    public:
        apply (_Fun fun, _RawFun arg)
            : _fun(fun), _arg(arg) { }

        _Res operator() (_Args... args)
        { return _fun(_arg, args...); }
    };

private:
    _Fun _fun;

public:
    explicit curry (_Fun fun)
        : _fun(fun) { }

    _Ret operator() (_Arg arg)
    { return _Ret(apply(_fun, decurry<_Arg>(arg))); }
};

The test code is a specified in the question: 测试代码是在问题中指定的:

int test(std::function<int(int,int)> f, int x)
{ return f(3, 4) * x; }

int main ()
{
    auto func_xy = curry<int(int(int,int),int)>(test);
    auto plus_xy = curry<int(int,int)>(std::plus<int>());
    auto func_px = func_xy(plus_xy);
    auto func_p5 = func_px(5);
    std::cout << func_p5 << std::endl;

    return 0;
}

The code output is on Ideone.com again. 代码输出再次在Ideone.com上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM