Allow Explicit Pointer for Auto Declaration

Olivier Grant via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 30 02:21:24 PDT 2016


On Friday, 30 September 2016 at 05:01:52 UTC, Steven 
Schveighoffer wrote:
> On 9/29/16 9:48 PM, Jacob wrote:
>> It still requires the &, what it prevents is this situation:
>>
>> auto pValue = expr; // wanted pointer, expr evaluates to 
>> non-ptr value
>> through change of code or simply forgetting "&"
>
> Wait, what happens when you do that? If that's now an error, 
> this is a non-starter.
>
> -Steve

Jacob is simply asking that you may explicit the fact that you 
expect the right hand side of the assignment to evaluate to a 
pointer while still using the type deduction offered by 'auto'.

auto p = expr;

Depending on the return type of expr, p's type can be either a 
pointer or a copy.

auto *p = expr;

Imposes that expr evaluate to a pointer.

Although the former is valid in C++ whatever expr evaluates to, I 
always use the latter when I expect expr to evaluate to a 
pointer. It also add consistency with 'auto &'.


More information about the Digitalmars-d mailing list