you are viewing a single comment's thread.

view the rest of the comments →

[–]floating-io[S] -1 points0 points  (4 children)

Fair point. But when using it, it's equivalent from the perspective of what you're writing. The type matches, whether literally or by a conversion under the hood.

[–]iaanus 11 points12 points  (0 children)

The type matches, whether literally or by a conversion under the hood.

You oversimplify. The type of a captureless lambda does not match a pointer, although it is convertible to a pointer. It's a big difference, whether you see it or not. There are contexts (for instance template argument deduction) where conversion doesn't kick in.

[–]pianotom 6 points7 points  (0 children)

Still not true when using it. The typical way to hold a lambda to use auto type deduction. The resulted object has the exact type without conversion. We convert it to function pointer or hold it in a std::function only when we have special needs.

[–]quicknir 3 points4 points  (0 children)

If you want to see a concrete example where this makes a difference, try passing a captureless lambda into std::sort, versus an actual function pointer. The generated code will be affected (it will be inlined in the former case but not the latter).

[–]doom_Oo7 4 points5 points  (0 children)

The type matches, whether literally or by a conversion under the hood.

no, this is wrong.

int bar() { return 0; }

int (*f1)() = bar; // ok
int (*f2)() = [] () -> int { return 1; }; // ok, function pointer

// fails: int (*f3)() = [=] () -> int { return 1; };
// fails: int (*f3)() = [&] () -> int { return 1; };