Compiler bug or incorrect usage for pointer of Struct?
Heromyth
bitworld at qq.com
Sun Jan 14 04:02:09 UTC 2018
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: [...]
>> auto writerFor(OutRange)(auto ref OutRange outRange)
>> {
>> auto res = Writer!(OutRange)();
>> res.setSink(outRange);
>> return res;
>> }
>>
>> struct Writer(OutRange)
>> {
>> private OutRange* output;
>>
>> void setSink(ref OutRange output)
>> {
>> this.output = &output;
> [...]
>
> Here's the bug. `output` refers to a local variable
> (parameter) in
> writerFor(), which goes out of scope after writerFor() exits, so
> this.output becomes a dangling pointer.
>
>
> T
I have another test. It runs whithout any error. Here it is:
import std.stdio;
void main()
{
Tester tester = new Tester(buildWriter());
tester.run("It's OK");
}
struct StringWriter
{
void put(string s)
{
writeln(s);
}
}
StringWriter buildWriter()
{
return StringWriter();
}
class Tester
{
private StringWriter* writer;
this(StringWriter w)
{
writer = &w;
writer.put("ok");
}
void run(string m)
{
writer.put(m);
}
}
More information about the Digitalmars-d
mailing list