const int vs. int const
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Fri Aug 15 11:47:50 PDT 2014
On 8/15/2014 10:45 AM, H. S. Teoh via Digitalmars-d wrote:
> Nah, the better practice is to write const(int) instead of const int,
> which is ambiguous when used to specify a function's return value.
>
> For example,
>
> const int func();
>
> is *not* the same as:
>
> const(int) func();
>
> which can be quite a nasty surprise for the unwary. My personal practice
> is to always write const(int) everywhere, thus eliminating the
> ambiguity.
const without parentheses is a 'storage class'.
const with parentheses is a 'type constructor'.
Storage classes apply to the symbol being declared, and hence the type of the
symbol.
Type constructors apply to the type in the ( ).
It is not ambiguous from a semantic or syntactic point of view, but it appears
to be ambiguous for those coming from C++. This was discussed at length a few
years ago, but no solution emerged that didn't make things much worse.
More information about the Digitalmars-d
mailing list