C++ expected-to-fail compilation goes through by not detecting mutable-specifier on lambda...

leon zadorin leonleon77@gmail.com
Mon Mar 2 02:57:00 GMT 2020


Hi, a quick question on whether GCC/clang are doing it correctly or whether
MSVC is more correct...

Given the following:

template<typename T>
void foo(T const f)
{
    f();
}

struct S {
    void operator()()
    {
    }
};

int main()
{
    foo(S());
}

... the above fails to compile as expected (need to add 'const' qualifier
to operator()() to make it work).

However, if replacing 'struct s' with similar lambda semantics, the effect
of 'mutable' (i.e. making operator()() non-const) are not detected by
GCC/clang to a point where both of the following compile ok:
int main()
{
    foo([](){});
    foo([]() mutable {});
}

... whereas MSVC will compile (as expected)
   foo([](){});
but will barf (which feels also more, it least intuitively, correct) on
   foo([]() mutable {});
(because latter has non-const lambda's operator()()) ...

So I was just wondering whether MSVC is correct or may be its just me and I
need to read more and proper about C++ standard on how lambda's mutable
specifier is treated in various contexts? :) or may be I just need to brush
up on C++ standard altogether :)

Best regards
Leon.



More information about the Gcc-help mailing list