Ok a follow-up to my previous email, it has nothing to do with opAssign in
particular. Any function that takes an argument by value, pass it the result
of a function that returns a reference and it won't call the postblit.
Calling it with a ref argument on the other hand works properly:

import std.stdio;

struct Test {
this( this ) { writeln( "Test.this(this)" ); }
~this()      { writeln( "Test.~this()" );    }
}

ref Test returnsRef( ref Test x ) { return x; }
void takesVal( Test x ) {}
void takesRef( ref Test x ) {
     takesVal( x );
}

int main() {
Test x;
 writeln( "-- with ref argument (works)" );
 takesRef( x );
 writeln( "-- with ref return (doesn't work)" );
 takesVal( returnsRef( x ) );
 writeln( "-- done");
 return 0;
}

I'm starting to be pretty sure this is a bug.
---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco



On 23 June 2011 19:37, Cristi Cobzarenco <cristi.cobzare...@gmail.com>wrote:

> ------------------------------
> import std.stdio;
>
> struct Test {
> this( this )              { writeln( "Test.this(this)" ); }
>  ~this()                   { writeln( "Test.~this()" );    }
> void opAssign( Test rhs ) { writeln( "Test.opAssign" );   }
> }
>
> ref Test makeRef( ref Test test ) { return test; }
>
> int main(string[] argv) {
> Test x,y;
>  writeln( "Before assignment" );
> x = makeRef(y);
>  writeln( "After assignment" );
>  readln();
>  return 0;
> }
>
>
> ------------------------------
> Output:
>
> Before assignment
> Test.opAssign
> Test.~this()
> After assignment
> Test.~this()
> Test.~this()
> ------------------------------
>
> The code above assigns a reference to a Test object to another Test object.
> The assignment  doesn't call the postblit constructor, yet it calls the
> destructor (lines 2,3 in the output). Changing the assignment to "x = y;"
> makes it call the postblit.
> Is this correct behaviour, or should I file it as a bug?
>
> ---
> Cristi Cobzarenco
> BSc in Artificial Intelligence and Computer Science
> University of Edinburgh
> Profile: http://www.google.com/profiles/cristi.cobzarenco
>
>

Reply via email to