"<Type> is not mutable" when using ref return type

Steven Schveighoffer schveiguy at yahoo.com
Thu May 19 04:24:39 PDT 2011


On Thu, 19 May 2011 00:22:42 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On 2011-05-18 20:55, %u wrote:
>> Hi!
>>
>> Is this a bug, or is it intentional that this fails? I can't come up
>> with any case where it would cause a problem, but the compiler doesn't
>> like the fact that there's a const in the structure:
>>
>>     struct Temp { const int a; int b; }
>>
>>     auto ref foo(Temp* t) { return *t; } //Error
>
> As soon as you do *t, you're copying the value.

That's not true, if it's ref, it just copies the reference.

example:

void foo(ref int i) { i = 5; }

void main()
{
    int i;
    int *p = &i;
    foo(*p);
    assert(i == 5);
}

To further drive home the point, this actually compiles:


Temp foo(Temp* t) { return *t; } // no error

There should be no restrictions on returning ref there, no matter what's  
inside Temp.

Definitely a bug with the original, please file with bugzilla.

-Steve


More information about the Digitalmars-d mailing list