Const ref and rvalues again...

luka8088 luka8088 at owave.net
Tue Nov 13 00:34:18 PST 2012


On 13.11.2012 2:16, martin wrote:
> On Monday, 12 November 2012 at 23:38:43 UTC, luka8088 wrote:
>> What about making this a default behavior and introducing a new
>> keyword if the function wants to modify the argument but it is not ref
>> (pass by value) ? The reason I think that this should be a default
>> behavior because not many functions actually modify their arguments
>> and so it leaves a lot of space for optimization.
>>
>> For example:
>>
>> void f (int x, val int y, ref int z) {
>> x = 1; // x is not copied
>> // compiler throws an error, x is not passed by value
>> // and therefor could not / should not be changed
>> y = 2; // ok, y is copied
>> z = 3; // ok, z is a reference
>> }
>
> Your proposal isn't really related to this thread's topic, but I

Um, "Const ref and rvalues again", I suggest it to be the default 
behavior, how is this not related ?

> understand what you mean (although your code comments distract me a bit):
>
> void f(const int x, int y, ref int z); =>
> void f(int x, val/mutable int y, ref int z);
>

Yes, you understood correctly:
void f (const ref int x, int y, ref int z); =>
void f (int x, val int y, ref int z);

The point here is to make "We need a way for a function to declare that 
it doesn't want it's argument to be copied, but it also doesn't care 
whether the argument is an rvalue or an lvalue. " a default behavior.

> I use const/in ;) parameters a lot in my code too to prevent accidental
> modifications, so my function signatures may be more compact by treating
> normal pass-by-value parameters as const if not denoted with a special
> keyword. I guess it wouldn't be very important for optimization though
> because I'd expect the optimizer to detect unchanged parameters. Anyway,
> your proposal would completely break existing code.

Would it ? How many functions actually change their non ref/out 
arguments ? Can you point out any existing public code that would be 
broken ?



More information about the Digitalmars-d mailing list