bug : consecutive function calls and -inline

Sivo Schilling sivo.schilling at web.de
Sat May 24 08:10:12 PDT 2008


noob-is-noob Wrote:

> janderson wrote:
> > noob-is-noob wrote:
> >>> sorry if it has been discussed.
> >>>> <snip old code>
> > 
> > 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:
> > < snip old code >
> > How that helps.
> > -Joel
> 
> Thank you.
> But it seems not related to array copying.
> I've tried with a more basic data type.
> 
> ==code==
> import std.stdio ;
> class A {
>   int c = 0 ;
>   A opCall(int b) { c = c + b ; writefln(c) ; return this ; }
> }
> struct B {
>   int c = 0 ;
>   B opCall(int b) { c = c + b ; writefln(c) ; return *this ; }
> }
> void main() {
>   A a = new A ;
>   B b ;
>   a(1)(2)(3) ;
>   a(0) ;
>   b(1)(2)(3) ;
>   b(0) ;
> }
> ==output=== (edited)
> -inline version:
> 1 3 6 6   <- class A
> 1 3 6 6   <- struct B
> non-inline version:
> 1 3 6 6   <- class A
> 1 3 6 1   <- struct B
> 
> sorry for my bad English.
> 
> 
I'm not sure that this will suitable to you but if you replace
the struct in your code with a pointer to a struct it works.
Ok it looks ugly but you can use opCall only on structs not 
pointer to structs.

===code===
module structptr;

import std.stdio;

struct S
{
    int c;

    S* opCall(int cc)
    {
        c += cc;
        writef(c, " ");
        return this;
    }
}

void main()
{
    // use an S on the heap
    auto t = new S;
    (*(*(*t)(1))(2))(3);
    (*t)(0);

    writefln("\n========");

    // use an S on the stack
    S s;
    S* sp = &s;
    (*(*(*sp)(1))(2))(3);
    (*sp)(0);
}
===end of code===

output (compiled without -inline !):
1 3 6 6
========
1 3 6 6

as expected. 
With classes you don't go into such problems because they are
always references to heap or stack allocated memory.

Regards.


More information about the Digitalmars-d-learn mailing list