www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Sort! string[][]

reply Brian Myers <bmyers harryanddavid.com> writes:
The sort! template in std.algorithm doesn't seem to work. The following code:

import std.string;
import std.algorithm;

void main(char[][] args)
{
    string[] words = ["c", "ab", "bb"];
    sort!("a < b", SortStrategy.stable)(words);
    assert(words == ["ab", "bb", "c"]);
}

This code gives the following error:

C:\Documents and Settings\dag033\D\d_dedup>dmd -g hello.d
hello.d(10): template std.algorithm.sort(alias less,SwapStrategy ss =
SwapStrategy.unstable,alias iterSwap = .iterSwap,R
ange) does not match any function template declaration
hello.d(10): template std.algorithm.sort(alias less,SwapStrategy ss =
SwapStrategy.unstable,alias iterSwap = .iterSwap,R
ange) cannot deduce template function from argument types !("a <
b",int)(invariant(char)[][])

I must admit I really haven't wrapped my head around the way templates work
yet. I certainly get the over all idea, but the subtleties of type deduction
and instantiation still elude me in non-trivial cases. What's going on here?

Brian
Apr 23 2008
parent Brian Myers <bmyers harryanddavid.com> writes:
Brian Myers Wrote:

 The sort! template in std.algorithm doesn't seem to work. The following code:
 
 import std.string;
 import std.algorithm;
 
 void main(char[][] args)
 {
     string[] words = ["c", "ab", "bb"];
     sort!("a < b", SortStrategy.stable)(words);
     assert(words == ["ab", "bb", "c"]);
 }
 
 This code gives the following error:
 
 C:\Documents and Settings\dag033\D\d_dedup>dmd -g hello.d
 hello.d(10): template std.algorithm.sort(alias less,SwapStrategy ss =
SwapStrategy.unstable,alias iterSwap = .iterSwap,R
 ange) does not match any function template declaration
 hello.d(10): template std.algorithm.sort(alias less,SwapStrategy ss =
SwapStrategy.unstable,alias iterSwap = .iterSwap,R
 ange) cannot deduce template function from argument types !("a <
b",int)(invariant(char)[][])
 
 I must admit I really haven't wrapped my head around the way templates work
yet. I certainly get the over all idea, but the subtleties of type deduction
and instantiation still elude me in non-trivial cases. What's going on here?
 
 Brian
 

Andrei Alexandrescu just responded noting that I was using SortStrategy instead of SwapStrategy as I should have been. He's filing a bug for the misleading error message. Thanx Andrei. Brian
Apr 23 2008