我試圖將D的sort
函數作爲模板參數發送到pipe
函數。當我使用sort
沒有模板參數工作原理: 將模板化函數作爲參數發送到D中的模板函數
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"
?對於我想要做的事情,有沒有正確的語法?
UPDATE
OK,我試圖包裹sort
功能。下面的代碼工作:
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
需要?
我看看......我想通了'pipe'模板,隱式得到的參數作爲模板參數的類型,應傳遞參數給第一管道的功能,但我看並非如此。 – 2012-03-10 19:17:06
@IdanArye:'pipe'永遠不會這樣做,因爲可以將它從參數中分離出來('alias pipe!(f)piped;'然後多行''pipeped([1,2,3]);') – kennytm 2012-03-10 19:54:42
不應該像''別名'使'管道'模板功能本身? – 2012-03-10 21:48:16