C++11 lambda as member variable?

后端 未结 5 1186
Happy的楠姐
Happy的楠姐 2021-01-31 07:24

Can lambda\'s be defined as class members?

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



        
5条回答
  •  北海茫月
    2021-01-31 07:51

    A lambda just makes a function object, so, yes, you can initialize a function member with a lambda. Here is an example:

    #include 
    #include 
    
    struct Example {
    
      Example() {
        lambda = [](double x) { return int(std::round(x)); };
      };
    
      std::function lambda;
    
    };
    

提交回复
热议问题