Templates: Array slices not recognized

Chris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 20 03:57:56 PDT 2015


On Monday, 20 April 2015 at 10:42:54 UTC, Chris wrote:
> On Monday, 20 April 2015 at 10:27:00 UTC, anonymous wrote:
>> On Monday, 20 April 2015 at 10:14:27 UTC, Chris wrote:
>>> string a = "bla";
>>> string b = "blub";
>>>
>>> auto res = doSomething(a, b);
>>>
>>> If I didn't use "auto ref" or "ref", string would get copied, 
>>> wouldn't it?
>>>
>>> auto ref doSomething(R needle, R haystack);
>>>
>>> To avoid this, I would have to write a[0..$], b[0..$], which 
>>> is not nice. Right or wrong?
>>
>> Wrong. `a[0..$]` is the same as simply `a`. A `string` is just 
>> another slice: a pointer and a length.
>
> Ah, I see. So strings don't get copied around and I can remove 
> ref, very good.

For the record. I mixed up the results there. The code executed 
directly in the for loop is faster than using a struct, it's also 
faster than using a mixin template (around 10 msecs). My 
apologies.


More information about the Digitalmars-d-learn mailing list