Understanding opAssign and 'alias this'

Maurice m-ou.se at m-ou.se
Sun Oct 13 14:37:29 PDT 2013


Can anybody explain my what is happening here?

enum xxx = true;

struct A {
	static if (xxx) ref A opAssign(ref const A a) { return this; }
	ref A opAssign(int v) { return this; }

struct B {
	A a;
	alias a this;

void main() {
	A a;
	B b;
	a = b; // [1]
	b = a; // [2]

When xxx is false:
[1] Gives an error
[2] Compiles fine

When xxx is true:
[1] Compiles fine
[2] Gives an error

What exactly is happening here? When xxx is false, what does 'b = 
a' do that is wrong when xxx is true?

I can't find any clear documentation about opAssign and its 
'default' implementation.



More information about the Digitalmars-d-learn mailing list