sort
Dの関数をテンプレート引数として関数に送信しようとしていpipe
ます。sort
テンプレート引数なし
で使用すると、次のように機能します。
import std.stdio,std.algorithm,std.functional;
void main()
{
auto arr=pipe!(sort)([1,3,2]);
writeln(arr);
}
ただし、sort
テンプレート引数を使用しようとすると、次のようになります。
import std.stdio,std.algorithm,std.functional;
void main()
{
auto arr=pipe!(sort!"b<a")([1,3,2]);
writeln(arr);
}
エラーが発生します-main.d(5): Error: template instance sort!("b<a") sort!("b<a") does not match template declaration sort(alias less = "a < b",SwapStrategy ss = SwapStrategy.unstable,Range)
なぜそれが起こるのですか?sort!"b<a"
それ自体で動作し、と同じ引数と戻り型を持っているsort
のに、なぜpipe
受け入れるのに受け入れsort
ないのsort!"b<a"
ですか?そして、私がやろうとしていることの正しい構文はありますか?
アップデート
sort
OK、関数をラップしようとしました。次のコードが機能します。
import std.stdio,std.algorithm,std.functional,std.array;
template mysort(string comparer)
{
auto mysort(T)(T source)
{
sort!comparer(source);
return source;
}
}
void main()
{
auto arr=pipe!(mysort!"b<a")([1,3,2]);
writeln(arr);
}
では、なぜ元のバージョンが機能しないのですか?これは、余分なテンプレートパラメータsort
が必要なためですか?