Is this a bug or am I doing something wrong?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 3 15:09:01 PST 2014


On 12/03/2014 03:02 PM, drsneed wrote:
> Check out http://dpaste.dzfl.pl/a5ada78fccf5
> If my function named "IWillNotCompile" is run, I get an error stating
> "Component!int' and 'Component!int' are not compatible."
> If my function named "IWillCompile" is run, there are no errors. They do
> the same thing, just written slightly differently.
>
> Any suggestions?

Unlike C++, rvalues cannot be bound to reference parameters even if 
reference to const.

Make the parameter 'auto ref' and it will compile:

	Component!(T) opBinary(string op)(auto ref Component!(T) rhs)

For auto ref, the function must be a template so that the compiler can 
generate by-reference and by-value versions of it depending on whether 
the argument is lvalue versus rvalue.

Here is my understanding of the issue:

   http://ddili.org/ders/d.en/lvalue_rvalue.html

Ali



More information about the Digitalmars-d-learn mailing list