Why do struct literals count as lvalues?
Steven Schveighoffer
schveiguy at yahoo.com
Fri Aug 19 05:16:10 PDT 2011
On Thu, 18 Aug 2011 18:56:01 -0400, bearophile <bearophileHUGS at lycos.com>
wrote:
> Jonathan M Davis:
>
>> I see _zero_ reason to
>> treat a newly constructed object as any different from one which is
>> returned
>> from a function.
>
> I have not followed fully this thread, so I am not sure what you are
> talking about, so sorry if I am misunderstanding the whole topic.
>
> I assume you want to disallow code like:
>
> struct Vect {
> float[4] data = 0.0;
> // lot of operator overloading here, all arguments are by ref
> }
> void foo(ref Vect f) {}
> void main() {
> foo(Vect([1,2,3,4]));
> }
>
> Such code is common. In foo() and in all the Vect operators ref is
> required for performance. This code is quite handy. Forcing me to define
> and assign a Vect to a temporary variable in main is not handy.
>
> Think about writing expressions that use Vects with operator
> overloading, that use ref everywhere. If you can't pass and give Vects
> defined inside the expressions the code becomes quite more hairy.
I believe auto ref is slated to fix this problem.
-Steve
More information about the Digitalmars-d
mailing list