On 2014-10-11 18:10:22 +0000, Adam D. Ruppe said:
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.
That's not entirely true. They're passed by value unless something
changed. By value in that the struct representation is passed. That
is to say, the length and a pointer.
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.
That's because string literals are stored in the data segment and are
immutable rvalues. You can't have a ref to an rvalue.