Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:54:44+00:00 2026-06-03T13:54:44+00:00

I just came up with an (yet another!) implementation of function currying in C++

  • 0

I just came up with an (yet another!) implementation of function currying in C++ using template metaprogramming. (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:

#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…

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

Then the curried function’s signature is…

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…

(((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:

This is the kind of function that should be passed to the constructor of 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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:54:45+00:00Added an answer on June 3, 2026 at 1:54 pm

    I have implemented a cheating version of a decurry class to demonstrate how you would go about implementing the specialization. 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. It should be possible to write a generic one, but I didn’t want to spend more time on it.

    The decurry implementation is:

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just came across an issue with my Wordpress template that I can't figure out.
I just came across with a problem using XmlDocument.LoadXml . The application was crashing,
The xcode 4 preview just came out today. Has anyone tried it yet? It
I'm using cocos2d-x to develop an iPhone game and then it just came to
Just came across this by Chris Coyier - http://css-tricks.com/examples/CSSTabs/ Can anyone explain me, how
I just came across a weird error: private bool GetBoolValue() { //Do some logic
I just came across an idea in The Structure And Interpretation of Computer Programs
I just came across few articles, while selecting a wiki for my personal site.
I just came across NetBeans site and found its PHP and Ruby IDEs. I
I just came across a Nintendo emulator written entirely in JavaScript on the interwebs,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.