Compiler bug or incorrect usage for pointer of Struct?

Temtaime temtaime at gmail.com
Sun Jan 14 14:12:55 UTC 2018


On Sunday, 14 January 2018 at 13:24:14 UTC, Heromyth wrote:
> On Sunday, 14 January 2018 at 08:05:34 UTC, Temtaime wrote:
>> On Sunday, 14 January 2018 at 04:02:09 UTC, Heromyth wrote:
>>> On Saturday, 13 January 2018 at 14:11:23 UTC, H. S. Teoh 
>>> wrote:
>>>> On Sat, Jan 13, 2018 at 12:22:17PM +0000, Heromyth via 
>>>> Digitalmars-d wrote: [...]
>
>>
>> https://run.dlang.io/is/RUHtqK
>> It's not ok dude
>> It runs because you don't use any variable inside the struct 
>> and because struct members are simple functions with hidden 
>> parameter
>
> Thanks. It's really dangerous to use a pointer to struct!
>
> I have created another test based on your code. See here 
> https://run.dlang.io/is/LOeMKG
>
> I add *ref* in the constructor and add a new template fucntion 
> writerFor. So, it goes back the scenario in my first post.
>
> Tester tester = new Tester(buildWriter()); 	// can't compile. 
> The compiler does the right thing.
>
> Tester tester = writerFor(buildWriter());	// Here is a bug, 
> because the compiler takes this!
>
> Am I right?

There's no bug in compiler.
"auto ref" can be NOT a reference. It depends on its parameter. 
RValues are passed by value.

given

writerFor(buildWriter());

it becomes

auto writerFor(OutRange)(OutRange outRange) // NO REF, parameter 
is on the stack
{
     auto res = new Tester(outRange);
     return res;
}

and after writerFor the returned object points on a variable 
inside writerFor which already died


More information about the Digitalmars-d mailing list