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 8288615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:18:57+00:00 2026-06-08T12:18:57+00:00

EDIT: I use curry below, but have been informed this is instead partial application.

  • 0

EDIT: I use curry below, but have been informed this is instead partial application.

I’ve been trying to figure out how one would write a curry function in C++, and i actually figured it out!

#include <stdio.h>
#include <functional>

template< class Ret, class Arg1, class ...Args >
auto curry(  Ret f(Arg1,Args...), Arg1 arg )
    -> std::function< Ret(Args...) >
{
    return [=]( Args ...args ) { return f( arg, args... ); };
}

And i wrote a version for lambdas, too.

template< class Ret, class Arg1, class ...Args >
auto curry(  const std::function<Ret(Arg1,Args...)>& f, Arg1 arg )
    -> std::function< Ret(Args...) >
{
    return [=]( Args ...args ) { return f( arg, args... ); };
}

The tests:

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

int main()
{
    auto f5 = curry( f, 5 );
    auto g2 = curry( std::function<int(int,int)>([](int x, int y){ return x*y; }), 2 );
    printf("%d\n",f5(3));
    printf("%d\n",g2(3));
}

Yuck! The line initializing g2 is so large that i might as well have curried it manually.

auto g2 = [](int y){ return 2*y; };

Much shorter. But since the intent is to have a really generic and convenient curry function, could i either (1) write a better function or (2) somehow my lambda to implicitly construct an std::function? I fear the current version violates the rule of least surprise when f is not a free function. Especially annoying is how no make_function or similar-type function that i know of seems to exist. Really, my ideal solution would just be a call to std::bind, but i’m not sure how to use it with variadic templates.

PS: No boost, please, but i’ll settle if nothing else.

EDIT: I already know about std::bind. I wouldn’t be writing this function if std::bind did exactly what i wanted with the best syntax. This should be more of a special case where it only binds the first element.

As i said, my ideal solution should use bind, but if i wanted to use that, i’d use that.

  • 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-08T12:18:58+00:00Added an answer on June 8, 2026 at 12:18 pm

    A lot of the examples people provided and that i saw elsewhere used helper classes to do whatever they did. I realized this becomes trivial to write when you do that!

    #include <utility> // for declval
    #include <array>
    #include <cstdio>
    
    using namespace std;
    
    template< class F, class Arg >
    struct PartialApplication
    {
        F f;
        Arg arg;
    
        constexpr PartialApplication( F&& f, Arg&& arg )
            : f(forward<F>(f)), arg(forward<Arg>(arg))
        {
        }
    
        /* 
         * The return type of F only gets deduced based on the number of arguments
         * supplied. PartialApplication otherwise has no idea whether f takes 1 or 10 args.
         */
        template< class ... Args >
        constexpr auto operator() ( Args&& ...args )
            -> decltype( f(arg,declval<Args>()...) )
        {
            return f( arg, forward<Args>(args)... );
        }
    };
    
    template< class F, class A >
    constexpr PartialApplication<F,A> partial( F&& f, A&& a )
    {
        return PartialApplication<F,A>( forward<F>(f), forward<A>(a) );
    }
    
    /* Recursively apply for multiple arguments. */
    template< class F, class A, class B >
    constexpr auto partial( F&& f, A&& a, B&& b )
        -> decltype( partial(partial(declval<F>(),declval<A>()),
                             declval<B>()) )
    {
        return partial( partial(forward<F>(f),forward<A>(a)), forward<B>(b) );
    }
    
    /* Allow n-ary application. */
    template< class F, class A, class B, class ...C >
    constexpr auto partial( F&& f, A&& a, B&& b, C&& ...c )
        -> decltype( partial(partial(declval<F>(),declval<A>()),
                             declval<B>(),declval<C>()...) )
    {
        return partial( partial(forward<F>(f),forward<A>(a)), 
                        forward<B>(b), forward<C>(c)... );
    }
    
    int times(int x,int y) { return x*y; }
    
    int main()
    {
        printf( "5 * 2 = %d\n", partial(times,5)(2) );
        printf( "5 * 2 = %d\n", partial(times,5,2)() );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the following code, which I have not been able
Edit: I can use Actionscript 3.0 and/or Java I have a bit of a
I have a telerik-radgrid where I use an editcolumn. For the edit-column I use
I've read posts about why you can't have a (Edit -- generic) (which use
Solution Edit: Turns out you can't use the PHP SDK to return the correct
I feel really dumb for asking this question, but here it goes. I have
Lately I have been seeing an increasing number of design articles encouraging the use
I use edit-in-place plugin: http://arashkarimzadeh.com/jquery/7-editable-jquery-plugin.html I't works Great! I just need something to check
I choose an item in my list to edit (I use Context Menu) ,
I'm beginning to work on mailing-list software we use internally (EDIT: though we send

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.