A seasonal followup to “When is *x
also &x
?”
*x
also &x
?”- Teach the logic behind C-style declarations.
int *p
can mean “p
is anint*
,” and it can equally well mean “*p
is anint
.”int const *f()
can mean “You aren’t allowed to modify theint
,” or it can equally well mean “You aren’t allowed to modify*f()
.” (Of course this only works for pointers, not references; that’s another reason it’s important to reveal pointers early.)
In the Reddit comments, commenter “redditsoaddicting” pointed out that this logic even works for multiple declarations on the same line:
In
int *p, q;
, the expression*p, q
is of typeint
.
Not only that, but after const int& x(), *y;
, we find
that decltype(x(), *y)
is const int&
!
Posted 2020-04-01