Re:C++20 Reference Card
by thestoicattack on Wednesday January 29, 2020 @12:43PM
(#22183460)
To avoid this insidious bug:
versus the correct
[ Parent
| on HN ]
Note for people who don’t see the difference: if a guard doesn’t have a name, it won’t survive until the end of the scope, thus doesn’t guard anything.
I might be wrong here, but I think that the first one is a declaration of a guard with the name m using the default constructor.
The same way that int (a); and int a; are equivalent.
So the guard should remain until end of scope, but won't guard anything.
Lock guard has no default constructor, so the line will in fact materialize a temporary lock guard around the mutex m, then throw it away at the semi-colon, thus locking nothing.