const ref parameters and r-value references
Mark Isaacson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri May 2 01:17:06 PDT 2014
I'm in the process of learning/practicing D and I noticed
something that seems peculiar coming from a C++ background:
If I compile and run:
void fun(const ref int x) {
//Stuff
}
unittest {
fun(5); //Error! Does not compile
}
I get the specified error in my unit test. I understand that the
cause is that I've attempted to bind ref to an r-value, what's
curious is that in C++, the compiler realizes that this is a
non-issue because of 'const' and just 'makes it work'. Is there a
rationale behind why D does not do this? Is there a way to write
'fun' such that it avoids copies but still pledges
const-correctness while also allowing r-values to be passed in?
Thanks in advance!
More information about the Digitalmars-d-learn
mailing list