Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does []mutable{} not compile?

This compiles:

[]{};

This as well:

[]() mutable {};

But with this code, compilers throw error messages at me:

[] mutable {};
   ^~~~~~~
error: lambda requires '()' before 'mutable'

Is there any particular reason why?

like image 523
Ralph Tandetzky Avatar asked Nov 11 '16 14:11

Ralph Tandetzky


1 Answers

It's just a consequence of the way the grammar is written in the standard. I don't know if this is an oversight or not.

A lambda expression starts with a lambda introducer (the brackets), followed by an optional lambda declarator.

The lambda declarator contains the argument list, mutable, attributes, exception specifier and return type. All these are optional, except for the argument list. So if a lambda declarator is present at all, the parentheses must be there.

This is why you can't only have the mutable keyword.

like image 158
Sebastian Redl Avatar answered Oct 10 '22 06:10

Sebastian Redl