C ++中奇怪的括号括号表示法,每个循环看起来都像一个 [英] Strange bracket-parentheses notation in C++, looking somewhat like a for each loop
问题描述
这就是代码的样子:
auto generateHash = [](std::vector<File> &files) -> std::shared_ptr<std::string> {
// Other code here
}
这是什么意思?每个循环都可以吗?一开始的括号是做什么的?括号是做什么的?箭头是什么意思?由于没有C ++ 11编译器,我无法编译它,并且在C ++参考中找不到它.
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.
推荐答案
这是什么意思?
What does this mean?
这是一个 lambda -一个函数对象.您可以像调用带有文件向量的函数一样调用它(由于某些奇怪的原因而通过可变引用传递),并且它返回一个字符串(由于某些奇怪的原因而由共享指针进行管理).
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?
不.看起来像
for (auto thing : sequence) {/* code */}
开头的括号是做什么的?
What do the brackets in the beginning do?
他们表示这是lambda.它们可以包含要在lambda内部的代码中使用的局部变量的捕获列表.
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?
这是跟踪返回类型.在C ++ 11中,如果需要,可以将该语法与普通函数一起使用.但这是指定lambda的返回类型的唯一方法.
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.
我在C ++参考中找不到它.
I can't find it in the C++ reference.
就在这里: http://en.cppreference.com/w/cpp/语言/lambda
这篇关于C ++中奇怪的括号括号表示法,每个循环看起来都像一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!