Templates: Array slices not recognized
Chris via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 20 03:14:25 PDT 2015
On Monday, 20 April 2015 at 09:58:06 UTC, Marc Schütz wrote:
> On Monday, 20 April 2015 at 09:07:54 UTC, Chris wrote:
>> On Saturday, 18 April 2015 at 17:59:19 UTC, ketmar wrote:
>>> On Sat, 18 Apr 2015 17:50:56 +0000, Chris wrote:
>>>
>>>> Doh! You're right! My bad. However, this makes the function
>>>> less
>>>> generic, but it doesn't matter here.
>>>
>>> maybe `auto ref` can help here?
>>
>> Yes, auto ref does the trick. I prefer it to passing the slice
>> by value, because I can extend the function to cater for more
>> types. My implementation (which is different from the one
>> posted above) works also with strings, for example. Anyway, it
>> gave me a performance boost of ~2.7 times as compared to the
>> workarounds I had. This was well worth it!
>
> Strings are slices, too. `string` is equivalent to
> `immutable(char)[]`.
I know. My function returns an array of the same type (size_t[]
or a string (immutable(char)[] or whatever). With auto ref I can
pass a string without having to slice it, like so
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?
For the record, I put the function inside a struct which again
boosted the performance. No it is faster by a factor of ~3.17.
Using the struct is even slightly faster than executing the same
code directly within the for loop. That surprised me.
More information about the Digitalmars-d-learn
mailing list