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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:11:39+00:00 2026-06-04T10:11:39+00:00

In C++11, how do you declare a function that takes a lambda expression as

  • 0

In C++11, how do you declare a function that takes a lambda expression as an argument? I can find plenty of resources online for declaring lambdas or taking them as template parameters, but what I’d really like to do is be able to make use of lambdas as easy-to-declare callback handlers, similar to what’s made possible by closures in JavaScript and code blocks in Objective-C.

Essentially, the classic C++ construct I want to replace with a lambda is something like:

class MyCallback {
public:
    virtual ~MyCallback() {}
    virtual void operator(int arg) = 0;
};

void registerCallback(const std::shared_ptr<MyCallback> &);

void foo(void) {
    int a, b, c;
    class LocalCallback: public MyCallback {
        int a, b, c;
    public:
        LocalCallback(int a, int b, int c): a(a), b(b), c(c) {}
        void operator(int arg) { std::cout << (a+b+c)*arg << std::endl; }
    };
    registerCallback(std::shared_ptr<MyCallback>(new LocalCallback(a,b,c)));
}

which would be simplified into:

void registerCallback(/* WHAT GOES HERE? */);

void foo(void) {
    int a, b, c;
    registerCallback([=](int arg){std::cout << (a+b+c)*arg << std::endl; })
}

So, what goes where I have written /* WHAT GOES HERE? */?

EDIT: This is for the purpose of storing a callback to be called back later, rather than for it being immediately consumed and called.

  • 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-04T10:11:40+00:00Added an answer on June 4, 2026 at 10:11 am

    Usually const std::function<void(int)> & or std::function<void(int)>.

    I’m not sure what the verdict is on whether std::function should be passed by const reference or by value. Probably by value is fine, especially since you’re going to copy it anyway to store.

    In case it isn’t clear in the middle of all that syntax, void(int) is a function type, and std::function<T> means approximately, “a functor with the same signature as functions of type T”.

    Lambdas themselves have anonymous types. There is no way to name the type of your lambda expression, and the types of different lambda expressions with the same signature are different:

    auto foo = [=](int arg){std::cout << (a+b+c)*arg << std::endl; };
    auto bar = [=](int arg){std::cout << (a+b+c)*arg << std::endl; };
    // foo and bar have different types, accessible as decltype(foo), decltype(bar)
    

    Hence the need for std::function, which basically is a type-erasing wrapper to gather together different functors with the same signature into a common type. It’s the bridge between static polymorphism with templates, and the dynamic polymorphism you need if you want to register a callback, store it for later, and then call it without having “remembered” the original type.

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

Sidebar

Related Questions

I need to declare a function in F# that takes 2 parameters (row, col)
I'm trying to declare a function that takes a list of records inside a
Can you call a function that has no body but is declared in the
I can forward declare a function in a namespace by doing this: void myNamespace::doThing();
can we declare a function in a header file in following way? extern int
I have a dll function that takes BSTR parameters. These are cast as char*
I am trying to build a simple worker function that takes a config of
I have a function that takes 2 parameters: @iEmployeeID and @dDate . It's purpose
I made the following function in SQL Server 2008 earlier this week that takes
I need to implement a functor that takes any (!) function pointer when instantiated,

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.