2

So this is how the code looks:

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

What does this mean? Is it a for each loop? What do the brackets in the beginning do? What do the parentheses do? What does the arrow mean? I can't compile it because of no C++11 compiler, and I can't find it in the C++ reference.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
DonAlonzo
  • 77
  • 1
  • 6
  • Its a [lambda](http://en.cppreference.com/w/cpp/language/lambda). First part ([]) is capture group, second (()) is arguments, third (->) is return type, then it is just the code. – Xarn Feb 26 '14 at 10:34
  • It is called lambda expression: http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 – Daniele Feb 26 '14 at 10:35

3 Answers3

7

What does this mean?

It's a lambda - a function object. You can call it like a function with a a vector of files (passed by mutable reference, for some weird reason), and it a returns string (managed by a shared pointer, for some weird reason).

std::vector<File> files = get_some_files();
std::shared_ptr<std::string> hash = generateHash(files); // call the lambda

Is it a for each loop?

No. That looks like

for (auto thing : sequence) {/* code */}

What do the brackets in the beginning do?

They signify that it's a lambda. They can contain the capture list of local variables that you want to make available to the code inside the lambda.

What does the arrow mean?

That's a trailing return type. In C++11, you can use that syntax with normal functions if you want; but it's the only way to specify a lambda's return type.

I can't find it in the C++ reference.

It's right here: http://en.cppreference.com/w/cpp/language/lambda

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • That helps a ton, thanks! Is there any possibility of doing this in another way? I can't change the compiler to a C++11 one, I'm afraid. – DonAlonzo Feb 26 '14 at 10:44
  • 1
    @DonAlonzo: You can probably replace it with a normal function, if it doesn't need to capture anything. If it does, then you'll probably need to write a function class with a constructor that captures whatever is needed. You'll have to get rid of `shared_ptr` too, since that's also C++11 - but it would make more sense to return `string` by value anyway. – Mike Seymour Feb 26 '14 at 10:47
1

This is lambda function, see e.g. http://en.cppreference.com/w/cpp/language/lambda.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
1

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<File> &files and returns std::shared_ptr<std::string>, 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.

Necrolis
  • 25,836
  • 3
  • 63
  • 101