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

  • Home
  • SEARCH
  • 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 6131869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:01:52+00:00 2026-05-23T17:01:52+00:00

Can lambdas be defined as data members? For example, would it be possible to

  • 0

Can lambdas be defined as data members?

For example, would it be possible to rewrite the code sample below using a lambda instead of a function object?

struct Foo {
    std::function<void()> bar;
};

The reason I wonder is because the following lambdas can be passed as arguments:

template<typename Lambda>
void call_lambda(Lambda lambda) // what is the exact type here?
{ 
    lambda();
}

int test_foo() {
    call_lambda([] { std::cout << "lambda calling" << std::endl; });
}

I’ve figured that if a lambda can be passed as a function argument, then maybe they can also be stored as a data member.

After more tinkering I found that this works (but it’s kind of pointless):

auto say_hello = [] { std::cout << "Hello"; };

struct Foo {
    using Bar = decltype(say_hello);
    Bar bar;
    Foo() : bar(say_hello) {}
};
  • 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-05-23T17:01:52+00:00Added an answer on May 23, 2026 at 5:01 pm

    Templates make it possible without type erasure, but that’s it:

    template<typename T>
    struct foo {
        T t;
    };
    
    template<typename T>
    foo<typename std::decay<T>::type>
    make_foo(T&& t)
    {
        return { std::forward<T>(t) };
    }
    
    // ...
    auto f = make_foo([] { return 42; });
    

    Repeating the arguments that everyone has already exposed: []{} is not a type, so you can’t use it as e.g. a template parameter like you’re trying. Using decltype is also iffy because every instance of a lambda expression is a notation for a separate closure object with a unique type. (e.g. the type of f above is not foo<decltype([] { return 42; })>.)

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

Sidebar

Related Questions

I can define church numerals fairly easy using scheme: > (define f (lambda (x)
In my repository implementation I can run the following query using a lambda expression:
I have a question regarding C++0x lambdas. In my code, it would be beneficial
I'm trying write a code that can the set property value through the lambda
I recently discovered that I can use lambdas to create simple event handlers. I
In c# (3.0 or 3.5, so we can use lambdas), is there an elegant
Can you use a lambda expressions as an argument to an attribute? The motivation
Can someone show me how to implement a recursive lambda expression to traverse a
I can't seem to find boost::lambda::ll for a nested ll::for_each() invocations in any header
how can I create a dynamic lambda expression to pass to use in my

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.