Calling a lambda expression multiple times

后端 未结 2 1693
隐瞒了意图╮
隐瞒了意图╮ 2021-02-15 15:07

I\'m trying the lambda-expressions from the new standard, and still don\'t understand them quite well.

Let\'s say I have a lambda somewhere in my code, e.g. in my main:<

相关标签:
2条回答
  • 2021-02-15 16:03

    You can store the lambda using auto, or assign it to a compatible std::function explicitly:

    auto f1 = [](int x, int y)->float{ ..... };
    std::function<float(int,int)> f2 = [](int x, int y)->float{ ..... };
    
    float x = f1(3,4);
    auto y = f2(5,6);
    

    You can always use f1 or f2 to construct or assign to a specific std::function type later on if necessary:

    std::function<float(int,int)> f3 = f1;
    
    0 讨论(0)
  • 2021-02-15 16:12
    auto lambda = [](int x, int y)->float
        {
            return static_cast<float>(x) / static_cast<float>(y);
        };
    // code
    // call lambda
    std::cout << lambda(1, 2) << std::endl;
    
    0 讨论(0)
提交回复
热议问题