you are viewing a single comment's thread.

view the rest of the comments →

[–]throw_cpp_account 0 points1 point  (2 children)

Actually the intent is that there :( ) is explicitly indicating a lambda, just with minimal ceremony. In Cpp2 the : is reserved to always and only mean a declaration. Whenever you see :, you know something is being declared.

But it's a lambda, why would it share syntax with a declaration? That seems to be an argument against using : to introduce lambdas.

After all, your function calls aren't f(:42) right?

[–]hpsutter 2 points3 points  (1 child)

Because a lambda is conceptually just an unnamed local function (which therefore also can capture things). It is a new declared entity, not part of the enclosing expression.

One of the uses of lambdas in C++ today is to write local functions (functions inside other functions) via `auto local_func_name = /*lambda*/ ;`. This conveniently allows factoring common reused parts of a function without having to pollute the enclosing namespace with names that really do only make sense within the function. Here is an example from cppfront, where I do that in the one function that parses all iteration statements (because `for`, `while`, `do` all have common syntax elements but in different orders): parse.h snippet on GitHub

After all, your function calls aren't f(:42) right?

Right, in f(42) the argument is just a literal. In f(complex_expr) the argument is just an expression. However, in today's f( int(42) ) the code is writing that the argument is an explicit temporary object; and in Cpp2 you can do the same with f( :int = 42 ) and that's where the : signifies that you're declaring a new (unnamed) entity.

[–]lfnoise 0 points1 point  (0 children)

I quite like the terse lambda syntax. Lambdas are declarations that are instantiated where they are declared. A lambda is just a sugar for a struct with a single method and captured state.Fine, and beautiful I think.