Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 03, 2014 auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
This doesn't work: bool less_than (T)(auto ref T a, auto ref T b) { return a < b; } Error: auto can only be used for template function parameters What am I doing wrong? Is this not a template function? |
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Sunday, 3 August 2014 at 19:07:32 UTC, Vlad Levenfeld wrote: > This doesn't work: > > bool less_than (T)(auto ref T a, auto ref T b) > { > return a < b; > } > > Error: auto can only be used for template function parameters > > What am I doing wrong? Is this not a template function? I think you can just skip 'auto'. Have a look at http://dlang.org/function.html , especially the function parameters section. |
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martijn Pot | This would make the function always take its argument by reference. I'm trying to use the feature here: http://dlang.org/template.html from the section Function Templates with Auto Ref Parameters |
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Sunday, 3 August 2014 at 19:07:32 UTC, Vlad Levenfeld wrote:
> bool less_than (T)(auto ref T a, auto ref T b)
> {
> return a < b;
> }
>
> Error: auto can only be used for template function parameters
Works for me with dmd versions back to 2.060. What compiler are
you using?
|
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Sunday, 3 August 2014 at 19:26:28 UTC, anonymous wrote:
> Works for me with dmd versions back to 2.060. What compiler are
> you using?
dmd 2.065
|
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Sunday, 3 August 2014 at 19:30:38 UTC, Vlad Levenfeld wrote:
> On Sunday, 3 August 2014 at 19:26:28 UTC, anonymous wrote:
>> Works for me with dmd versions back to 2.060. What compiler are
>> you using?
>
> dmd 2.065
Here's how I'm testing this:
$ dmd | head -n 1
DMD64 D Compiler v2.065
$ cat less_than.d
bool less_than (T)(auto ref T a, auto ref T b)
{
return a < b;
}
void main()
{
int a = 1, b = 2;
assert(less_than(a, b));
assert(less_than(a, 2));
assert(less_than(1, b));
assert(less_than(1, 2));
}
$ dmd less_than.d && echo ok
ok
If this exact code errors for you, it ... uhh ... could be
platform specific? Are you on Windows?
|
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Sunday, 3 August 2014 at 19:25:32 UTC, Vlad Levenfeld wrote:
> This would make the function always take its argument by reference. I'm trying to use the feature here:
>
> http://dlang.org/template.html
>
> from the section Function Templates with Auto Ref Parameters
I thought I finally saw a question I could (help) answer...
What is the benefit of 'auto ref' over 'in' as you are changing a nor b?
|
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Sunday, 3 August 2014 at 19:43:53 UTC, anonymous wrote:
> If this exact code errors for you, it ... uhh ... could be
> platform specific? Are you on Windows?
Debian, and your code works for me. I figured out the problem - I made less_than to serve as a default sorting predicate, so in a few places there is something like this:
void sort (R, T = ElementType!R, alias compare = less_than!T)(R range, T item)
{...}
And since auto ref parameters need actual parameters to deduce whether they should be ref or not, less_than!T isn't an instantiated template - its still an alias, because it still must choose between having ref and value parameters.
So, it looks like I can't use auto ref with less-than. Which is fine, if I need to quickly compare large structures I will just pass in a ref_less_than predicate or something.
|
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martijn Pot | On Sunday, 3 August 2014 at 20:10:39 UTC, Martijn Pot wrote:
> What is the benefit of 'auto ref' over 'in' as you are changing a nor b?
Because
less_than (T)(T a, T b)
(or in, or const T) will copy a and b on their way into the function. Usually this is ok but certain data structures I use are not intended to be copied, or they are just very large or have very elaborate constructors that take up time and resources, or something.
I don't know if inlining would solve this or not but as far as I know there is no way to rely on inlining currently.
|
August 03, 2014 Re: auto ref function parameters in a free function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Sunday, 3 August 2014 at 21:24:03 UTC, Vlad Levenfeld wrote:
> certain data structures I use are not intended to be copied, .....
although these cases are probably better off being compared by some kind of key rather than directly... so, auto ref isn't necessary here, it was just something I had tried and was surprised at the error message I got.
|
Copyright © 1999-2021 by the D Language Foundation