opAssign return type

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 2 10:33:22 PDT 2016


On 9/2/16 1:18 PM, Q. Schroll wrote:
> When overloading assignment, I've been taught in my C++ course to return
> a reference to the lvalue being assigned to. This is easily possible in
> D, but in Phobos it is used rarely and in Ali Çehreli's Book it is
> neither taught to do so.

Sure, you can do this.

>
> Is there any reason to it? To me it seems very obvious to do it like in
> C++.

I can imagine a reason is to avoid issues with lifetime. It's dangerous 
to return this as a reference in most cases, because this is implicitly 
passed by reference even for rvalues.

However, for opAssign, you generally are sure you don't have an rvalue.

In any case, there's nothing wrong with it technically, it should work.

-Steve


More information about the Digitalmars-d-learn mailing list