Allow Explicit Pointer for Auto Declaration
Jacob via Digitalmars-d
digitalmars-d at puremagic.com
Thu Sep 29 18:48:02 PDT 2016
On Friday, 30 September 2016 at 00:05:45 UTC, Steven
> On 9/29/16 7:42 PM, Jacob wrote:
>> Was wondering if this feature could be implemented, right now
>> auto needs
>> to be written like this:
>> auto pValue = someFunctionReturnsRef(); // forgot '&',
>> still valid
>> // makes a copy
>> when we
>> didn't want one
>> The feature would make the code look like this:
>> auto* pValue = someFunctionReturnsRef(); // error
>> expecting pointer
>> We don't accidentally make a copy when we wanted a pointer.
>> C++ has a
>> similar semantic for it's auto.
> Hm... I'm not familiar with C++ auto, but C++ does have local
> references. D does not.
> I'm not sure if you are asking for the syntax specified, or
> asking for the original line to be treated like the specified
> syntax. The latter absolutely is not going to happen, because
> that breaks valid code.
> Even if we added:
> auto* pValue = expr;
> I don't see why this is more advantageous than:
> auto pValue = &expr;
auto* pValue = expr; // still invalid code unless expr evaluate
to a pointer type
auto* pValue = &expr; // this is valid if expr is a ref
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 "&"
So no code should be broken, you can do everything the same way
without a change in behavior and auto with a "*" is currently
Basically how it works in C++: http://ideone.com/TUz9dO
More information about the Digitalmars-d