Understanding lvalue and rvalue
ANtlord via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Apr 27 21:46:00 PDT 2017
Hello! Short time ago I've met strange thing at least for me. I
have a non-copyable structure and two methods for it with same
name. I mean that I use function overloading.
First method takes rvalue of this structure. Second method takes
constant lvalue structure. But when I try to use this function
with instantiated object I get compile error.
struct MyStruct
{
@disable this(this);
int a;
}
void process(MyStruct obj) {
writeln("incoming rvalue");
}
void process(in ref MyStruct obj) {
writeln("incoming lvalue");
}
void main()
{
MyStruct obj = {a: 1};
process(obj);
}
Text of the error is "struct app.MyStruct is not copyable because
it is annotated with @disable"
Also I try to change word `in` by `const` and I get same result.
But when I remove `in` program is compiled. Is this mug or
feature. If feature, please help to understand it. Should I pass
structure instances by pointer? Like
void process(in MyStruct* obj) {
writeln("incoming lvalue");
}
void main()
{
MyStruct obj = {a: 1};
process(&obj);
}
Does make sense for me because it is more obvious in client code,
but I want to understand reason of error pointed above.
Thanks. Sorry if my English is not clear.
More information about the Digitalmars-d-learn
mailing list