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: [...]
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);
}
}
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