Why this doesn't compile?

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 11 11:10:22 PDT 2014


On Saturday, 11 October 2014 at 18:01:41 UTC, Uranuz wrote:
> When I want to pass generic String by const reference I get an 
> error.

Strings are passed by reference automatically, so you wouldn't 
want to double reference them anyway.

> As far as I understand const should accept both mutable and 
> immutable data. And there I want to pass it by reference.

Yes, but since "foo" isn't an lvalue (variable), you can't pass 
it by ref in D, even if it is const. This differs from C++, but I 
don't recall the reason, I think there's just too many weird edge 
cases that D wanted to avoid.

But in the case of strings, arrays, classes, pointers, and user 
defined structs that wrap these, you don't need to pass it as ref 
at all, just use plain const.


More information about the Digitalmars-d mailing list