Const ref and rvalues again...

Era Scarecrow rtcvb32 at yahoo.com
Tue Nov 6 17:34:55 PST 2012


On Wednesday, 7 November 2012 at 00:32:04 UTC, martin wrote:
> So we do not really need 'auto ref' for non-templated functions 
> or a new, even more confusing keyword which you, Jonathan, seem 
> to insist on - 'const ref' (or, more elegantly 'in ref') is all 
> we need. We simply want the compiler to automatically declare 
> rvalues passed to 'const ref' parameters as local lvalues - 
> problem solved for the majority of cases. For the remaining few 
> which care about the involved cost for rvalue arguments, allow 
> them to provide a function overload for rvalues (either 
> manually or via 'auto ref' templates) - problem solved 
> completely in my point of view. And that is actually exactly 
> what the thread starter Malte proposes (and I already pointed 
> that out in an earlier post).

  'in ref' may work, but what it implies doesn't quite make sense. 
'auto ref' already being used for templates should be able to 
carry over to functions, however auto in the same section along 
with type declaration may seem a little confusing. Let's glance 
at a few of how they would be called.

//either
void func(int x);
void func(const int x);

//lvalue
void func(ref int x);
void func(const ref int x);

//either? auto ref/in ref
void func(auto ref int x);
void func(const auto ref int x);
void func(in ref int x);

  Assuming auto can't be used to do type referencing the 'const 
auto ref int' seems rather long. alternatively perhaps inout? 
(accepts any type, partially skewed?)

void func(inout ref int x);
void func(const inout ref int x);

  At this point for const it's just as large as the const auto 
ref.. So..

void func(inout ref int x); //mutable
void func(in ref int x);    //const version

void func(out ref int x);   //optional write-only variable

  'out ref int x' could then be legal..  Somehow reminds me of 
zlib's compress utility function that returns how many bytes the 
output buffer used but sometimes you didn't care. So.. if we 
defined and used...


void func(out ref int x) {
   x = 100;
}

int x;
func(x);
assert(x == 100);

func(0);    //0 and null (and legal) basically ignored.
func(null);



More information about the Digitalmars-d mailing list