auto ref and arrays

Artur Skawina art.08.09 at gmail.com
Tue Nov 27 14:54:38 PST 2012


On 11/27/12 23:44, Ali Çehreli wrote:
> On 11/27/2012 02:09 PM, Jack Applegame wrote:
>> I don't understand why auto ref doesn't work with arrays.
>>
>> void test1(T)(auto ref const T[] val) {}
>> void test2(T)(auto ref const T val) {}
>> void main() {
>> int b;
>> test2(b); // OK
>> string a;
>> test1(a); // Error: cast(const(char[]))a is not an lvalue
>> }
>>
>> Since a is mutable itself, compiler uses ref storage class.
>> cast(const(char[]))a isn't an lvalue, so it's impossible to pass it by ref.
>>
>> But cast(const int)b isn't an lvalue too. Why it's no errors in this case?
> 
> Sorry, I am not answering the question but is 'auto ref' is legal as a parameter? It is not documented.

It's legal and documented: http://dlang.org/template.html

Whether the argument is mutable or not shouldn't matter -- compiler bug;
both examples should compile (limiting auto-ref to only modifiable lvalues
wouldn't make sense).

artur


More information about the Digitalmars-d-learn mailing list