Ralph Tandetzky Ralph Tandetzky - 5 months ago 37
C++ Question

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?


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.