bug : consecutive function calls and -inline

janderson askme at me.com
Sat May 24 01:48:52 PDT 2008


noob-is-noob wrote:
> sorry if it has been discussed.
> ===code===
> module evbug ;
> import std.stdio ;
> 
> struct S(T) {
>   T[] arr ;
>   S!(T) opCall(T a) {
>     arr ~= a ;
>     writefln("%s ", arr) ;
>     return *this ;
>   }
> }
> 
> void main() {
>   S!(string) t ;
>   t("AAA")("BBB")("CCC") ;
>   t("<-result") ;
> }
> ===output=== v2.014 (similar result for v1.030)
> 1/ compiled with -inline, ok:
> [AAA]
> [AAA BBB]
> [AAA BBB CCC]
> [AAA BBB CCC <-result]
> 
> 2/ compiled w/o -inline, ng:
> [AAA]
> [AAA BBB]
> [AAA BBB CCC]
> [AAA <-result]

It seems slightly odd that your implicitly copying T[].  I wonder if
there's even a defined behavior for copying of array indirectly by
struct.  I'd imagine that what the second version is doing is copying
the structure each time while the first is not.

So:
T[] a;
a.length = 10;
...
T[] b;
b.length = a.length;
b.ptr = a.ptr;
b.length += 5;

T[] c;
c.length = b.length;
c.ptr = b.ptr;
c.length += 5;

//a.length is 10
//b.length is 15
//c.length is 20

Now we try to modify the first element again (which is what your doing).

a.length += 5;

//a.length is now 15
//b.length is still 15
//c.length is still 20


It maybe that the second version is the required behavior or maybe the
compiler shouldn't let you copy structs implicitly if you have pointers
or arrays in them.

Tip. Try using pointers or a class.


More information about the Digitalmars-d-learn mailing list