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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:31:40+00:00 2026-06-13T17:31:40+00:00

////////////////////////////////////////////////////////////////// // Capture a lambda with a lambda and use it in the lambda

  • 0
    //////////////////////////////////////////////////////////////////
    // Capture a lambda with a lambda and use it in the lambda it's //
    // captured in along with some code in the lambda that captures //
    // it.                                                          //
    //                                                              //
    // Add to that to capture a variable in the client and use that //
    // too.                                                         //
    //                                                              //
    // Then make a lambda that captures a class object and calls    //
    // some method or methods with it, optionally modifies the re-  //
    // sult...                                                      //
    //////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////
    // Note: std::function<type(type)> f;                           //
    // f = <define lambda here>                                     //
    // f() //calls lambda                                           //
    //////////////////////////////////////////////////////////////////

    #include <functional>
    #include <iostream>
    #include <string>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::function;
    using std::getline;
    using std::string;

    class Experimental {
       private:
          int x;
          string s;
       public:
          Experimental() {}
          ~Experimental() {}
          void set_x(int new_x);
          int get_x();
          void set_s(string s_in);
          string get_s();
    };

    void Experimental::set_x(int new_x) {
       x = new_x;
    }

    int Experimental::get_x() {
       return (x);
    }

    void Experimental::set_s(string s_in) {
       s = s_in;
    }

    string Experimental::get_s() {
       return s;
    }

    int main() {
       double n;
       string input;
       Experimental* experiment = new Experimental();

       cout << "Enter a number: ";
       cin >> n;

       function<double(double)> f;
       f = [&f](double k) {
          return (k ? k * f(k-1) : 1);
       };

       function<double(double)> g;
       g = [&f,n](double m) {
          return (f(n)/n);
       };

       function<int()> T1; //capture a class and do stuff...
       T1 = [&experiment,n]() {
          experiment->set_x(13 + n);
          int m = experiment->get_x();
          return (m);
       };

       function<string(string)> T2; //capture a class and do stuff...
       T2 = [&experiment](const string in) {
          experiment->set_s(in);
          string s = experiment->get_s();
          return (s);
       };

       cout << "The factorial of " << n << " is: ";
       cout << f(n) << endl;

       cout << "The factorial of " << n << " divided by " << n << " is: ";
       cout << g(n) << endl;

       cout << "The new value of x in experiment is: ";
       cout << T1() << endl;

       cout << "Enter a string: ";
       getline(cin, input); //FIXME
    cout << "input is: " << input << "<-" << endl;

       cout << "The new string in experiment is: ";
       cout << T2(input) << endl;
       delete experiment;

       return (0);
    }

I know it’s ugly. Not reeally using lambdas as they’re meant to be used while I experiment here at first. For some reason I’m not getting input for my string variable and I don’t know why. Can someone help/ point out what the problem is?

  • 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-13T17:31:41+00:00Added an answer on June 13, 2026 at 5:31 pm

    The problem is your getline is reading the end-of-line left in the buffer after:

    cin >> n;
    

    Fix that by using ignore:

    #include <limits>
    ...
      cin >> n;
      ...
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      getline(cin, input);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C++0x, how do I capture a variable when I have a lambda within
I have a situation where I have a lambda as a member variable that
I know that .NET lambda expressions can capture outer variables. However, I have seen
I know that capture-parenthesed subexpressions is counted in back reference numbering even when they're
Basically I need to capture the video from videocamera do some processing on frames
Bash how do you capture stderr to a variable? I would like to do
When I use [=] to indicate that I would like all local variables to
I have a decent amount of code that relies on capturing a shared_from_this() when
I am trying to use a lambda to pass in place of a function
There are a lot of benefits for using lambda expression to capture property or

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.