Reputation: 11926
When i use the spiral rule, i am confused at below line within 10 spiral steps. Is there a quicker way?
// compiles in VC++ 2010
const void * const ** const volatile *** const **** _foo_;
Such as ptr is a pointer to a pointer to a pointer to a pointer and all of them are const void but 2 of them are volatile void const
Upvotes: 0
Views: 378
Reputation: 213160
const void * const ** const volatile *** const **** p
declare p as pointer to pointer to pointer to pointer to const pointer to pointer to pointer to const volatile pointer to pointer to const pointer to const void
Upvotes: 13
Reputation: 254711
With pointer and reference declarations, you just read from right to left:
ptr
is a pointer to a pointer to a pointer to a pointer to a const
pointer to a pointer to a pointer to a pointer to a const volatile
pointer to a pointer to a const
pointer to a const
object of unknown type.
If you follow the guideline of putting any const
or volatile
qualifiers after the first type specifier (i.e. void const *
) rather then before it (i.e const void *
), then you can read consistently from right to left; otherwise, you sometimes have a slight hiccough when you reach the left-hand end.
The spiral "rule" is occasionally useful for declarations involving arrays or functions, where the name being declared isn't the last thing in the declaration. In this case, with nothing after the name, it degenerates to reading right-to-left.
Upvotes: 7
Reputation: 154017
That's because there is no spiral rule. Basically, you process operands
on right first, then on the left, working outward in both cases, and
respecting parentheses. And cv-qualifiers normally qualify what's to
the left of them. Since this declaration has no operands on the right,
it's simply right to left: pointer to pointer to pointer to pointer to
const pointer to pointer to pointer to const volatile pointer to pointer
to const pointer to (const) void. The last const is because the final
const doesn't have anything to the left, so we have to treat the
declaration as if it were void const
, instead of const void
. Other
than that, the declaration should cause no problems, if you forget
about the misguided spirals.
Upvotes: 5