C++ reference type

Dave Dave_member at pathlink.com
Mon Jun 26 09:49:55 PDT 2006

BCS wrote:
> How about some sort of cast to l-value syntax that would be used from 
> the calling code. It would generate a copy and pass a reference to it.
> For that matter, any reason not to do that by default?

I don't think there is -- that's kinda where I was headed with this. I 
don't think the 'lvalue' limitation needs to be there because either way 
a temporary would already be created by the opCall (whether it is passed 
'in', 'out' or 'inout'). That is, the temporary is already created 
anyhow - why not just remove the restriction.

Besides the coding convenience, the reason I suggested the new 'byref' 
modifier is because the current 'lvalue' limitation kind-of makes sense 
in the context of 'out' or 'inout'. But there are lots of times when you 
may just want to pass something 'byref' not intending it to be modified 
(but that side effect _will_ still be there - I'm not suggesting some 
sort of C++-like const &).

- Dave

> Dave wrote:
> [...]
>> There's one more irritating limitation, and that is the ability to 
>> pass a temporary byref:
>> private import std.stdio;
>> void main()
>> {
>>     // error: (opCall)(100) is not an lvalue
>>     writefln(foo(MyStruct(100),100));
>> }
>> int foo(inout MyStruct s, int i)
>> {
>>     return s.i * i;
>> }
>> struct MyStruct
>> {
>>     private int i = 0;
>>     static MyStruct opCall(int i)
>>     {
>>         MyStruct s;
>>         s.i = i;
>>         return s;
>>     }
>> }

More information about the Digitalmars-d mailing list