Ah, sorry for the confusion, {} and () are mostly interchangeable when initializing objects. I didn't really mean to have the packaged_task and the function to be initialized differently, but in this case, you can freely swap {} and ().
The lambda [](){ return 42; }; is the argument of std::packaged_task. So either it can be std::packaged_task([](){ return 42; }); or std::packaged_task{[](){ return 42; }};
Right, I'm not talking about the parenthesis/curlybraces after packaged_task, I'm talking about the curly braces that enclose the whole assignment. Why are they necessary?
Ah, ok, now I understand. The scope (the curly braces) are not around the assignment, they are around the entire block demonstrating one variant. This allows the next variant to use the same variable names.
Question: why do you need to enclose that first assignment (auto c= ... ) in an extra set of curly braces?
Ah, sorry for the confusion, {} and () are mostly interchangeable when initializing objects. I didn't really mean to have the packaged_task and the function to be initialized differently, but in this case, you can freely swap {} and ().
Ok, but why are the surrounding curly braces needed there?
You have:
{
auto c = std::packaged_task{[](){
return 42;
}};
Why couldn't you just have:
auto c = std::packaged_task{[](){
return 42;
};
The lambda [](){ return 42; }; is the argument of std::packaged_task. So either it can be std::packaged_task([](){ return 42; }); or std::packaged_task{[](){ return 42; }};
Right, I'm not talking about the parenthesis/curlybraces after packaged_task, I'm talking about the curly braces that enclose the whole assignment. Why are they necessary?
Ah, ok, now I understand. The scope (the curly braces) are not around the assignment, they are around the entire block demonstrating one variant. This allows the next variant to use the same variable names.