Strange bracket-parentheses notation in C++, looking somewhat like a for each loop

后端 未结 3 1498
醉梦人生
醉梦人生 2021-01-22 08:33

So this is how the code looks:

auto generateHash = [](std::vector &files) -> std::shared_ptr {
    // Other code here
}
         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-22 09:23

    This is a C++11 lambda function, for a tutorial on how they work, you can look at this, else for a pure reference you can look at this.

    In your case it defines an anonymous function that takes in std::vector &files and returns std::shared_ptr, and assigns this function to generateHash. the auto keyword tells the compiler to derive the type of generateHash (it this case it makes for a simple shorthand). the empty brackets ([]) means that the lambda doesn't capture and local variables for use within the lambda.

提交回复
热议问题