digitalmars.D.learn - Why is the struct instance being copied here?
- d coder <dlang.coder gmail.com> Mar 11 2011
- "Steven Schveighoffer" <schveiguy yahoo.com> Mar 11 2011
--001517740b4c515dad049e31e7cf
Content-Type: text/plain; charset=ISO-8859-1
Greetings
Please look at the code down here. When compiled and run, I get the message
"Call to postblit" printed. I think it is because of the foreach block,
because the variable "i" is not declared as ref there. Is there a way to
make it a ref?
Regards
- Puneet
import std.stdio;
struct Foo {
this(this) {
writeln("Call to postblit");
}
}
class Bar {
Foo foo;
this() {
foreach(i, f; this.tupleof) {
// do nothing
}
}
}
void main()
{
Bar bar = new Bar();
}
--001517740b4c515dad049e31e7cf
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div>Greetings</div><div><br></div><div>Please look at the code down here. =
When compiled and run, I get the message "Call to postblit" print=
ed. I think it is because of the foreach block, because the variable "=
i" is not declared as ref there. Is there a way to make it a ref?</div=
<div><br></div><div>Regards</div><div>- Puneet</div><div><br></div><div>imp=
ort std.stdio;</div><div><br></div><div>struct Foo {</div><div>=A0 this(thi=
s) {</div><div>=A0 =A0 writeln("Call to postblit");</div><div>=A0=
}</div>
<div>}</div><div><br></div><div>class Bar {</div><div>=A0 Foo foo;</div><di=
v>=A0 this() {</div><div>=A0 =A0 foreach(i, f; this.tupleof) {</div><div>=
=A0 =A0 =A0 // do nothing</div><div>=A0 =A0 }</div><div>=A0 }</div><div>}</=
div><div><br></div>
<div>void main()</div><div>{</div><div>=A0 Bar bar =3D new Bar();</div><div=
}</div><div><br></div>
--001517740b4c515dad049e31e7cf--
Mar 11 2011
On Fri, 11 Mar 2011 04:50:38 -0500, d coder <dlang.coder gmail.com> wrote:Greetings Please look at the code down here. When compiled and run, I get the message "Call to postblit" printed. I think it is because of the foreach block, because the variable "i" is not declared as ref there. Is there a way to make it a ref? Regards - Puneet import std.stdio; struct Foo { this(this) { writeln("Call to postblit"); } } class Bar { Foo foo; this() { foreach(i, f; this.tupleof) { // do nothing } } } void main() { Bar bar = new Bar(); }
This typically works in a foreach loop: foreach(i, ref f; x) but a foreach for a tuple is a special beast, and using ref in your code yields this error: foreachtuple.d(12): Error: no storage class for value f But I agree it should be doable. This should qualify for an enhancement request: http://d.puremagic.com/issues/enter_bug.cgi?product=D -Steve
Mar 11 2011








"Steven Schveighoffer" <schveiguy yahoo.com>