digitalmars.D.learn - auto ref function parameters in a free function
- Vlad Levenfeld (7/7) Aug 03 2014 This doesn't work:
- Martijn Pot (4/11) Aug 03 2014 I think you can just skip 'auto'.
- Vlad Levenfeld (4/4) Aug 03 2014 This would make the function always take its argument by
- Martijn Pot (4/8) Aug 03 2014 I thought I finally saw a question I could (help) answer...
- Vlad Levenfeld (10/12) Aug 03 2014 Because
- Vlad Levenfeld (5/7) Aug 03 2014 although these cases are probably better off being compared by
- anonymous (3/8) Aug 03 2014 Works for me with dmd versions back to 2.060. What compiler are
- Vlad Levenfeld (2/4) Aug 03 2014 dmd 2.065
- anonymous (21/25) Aug 03 2014 Here's how I'm testing this:
- Vlad Levenfeld (14/16) Aug 03 2014 Debian, and your code works for me. I figured out the problem - I
- Artur Skawina via Digitalmars-d-learn (4/9) Aug 03 2014 void sort (R, T = ElementType!R, alias compare = less_than)(R range, T ...
- Vlad Levenfeld (3/8) Aug 03 2014 This works! Thanks!
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?
Aug 03 2014
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.
Aug 03 2014
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
Aug 03 2014
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 ParametersI 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?
Aug 03 2014
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.
Aug 03 2014
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.
Aug 03 2014
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 parametersWorks for me with dmd versions back to 2.060. What compiler are you using?
Aug 03 2014
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
Aug 03 2014
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: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?Works for me with dmd versions back to 2.060. What compiler are you using?dmd 2.065
Aug 03 2014
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.
Aug 03 2014
On 08/03/14 23:19, Vlad Levenfeld via Digitalmars-d-learn wrote: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) {...}void sort (R, T = ElementType!R, alias compare = less_than)(R range, T item) [should work iff the sort implementation only calls the predicate] artur
Aug 03 2014
On Sunday, 3 August 2014 at 21:47:06 UTC, Artur Skawina via Digitalmars-d-learn wrote:void sort (R, T = ElementType!R, alias compare = less_than)(R range, T item) [should work iff the sort implementation only calls the predicate] arturThis works! Thanks!
Aug 03 2014