次のコードを検討してください。
module ftwr;
import std.regex;
import std.stdio;
import std.conv;
import std.traits;
S consume (S) (ref S data, Regex ! ( Unqual!(typeof(S.init[0])) ) rg)
{
writeln (typeid(Unqual!(typeof(S.init[0]))));
auto m = match(data, rg);
return m.hit;
}
void main()
{
auto data = "binary large object";
auto rx = regex(".*");
consume (data, rx); // this line is mentioned in the error message
}
consume
今、私はコンパイラがそれがインスタンス化されることを推測することを期待しています
string consume!(string)(string, Regex!(char))
しかし、それは起こらないようです。エラーは次のとおりです。
func_template_with_regex.d(24): Error: template ftwr.consume(S) does not match any function template declaration
func_template_with_regex.d(24): Error: template ftwr.consume(S) cannot deduce template function from argument types !()(string,Regex!(char))
パラメータタイプが正しいことがわかります...次のような関数シグネチャのバリエーションをいくつか試しました。
S consume (S) (Regex ! ( Unqual!(typeof(S.init[0])) ) rg, ref S data)
これもコンパイルされません(引数の順序を変更するというアイデアでした)、そして
immutable(S)[] consume (S) (Regex ! ( S ) rg, ref immutable(S)[] data)
これは、型をコンパイルして推測します。呼び出しでタイプを明示的に指定した場合、つまり
consume!string(data, rx);
また、期待どおりにコンパイルされ、デバッグwriteln
が出力char
されます。推論規則に何かが欠けているのですか、それともコンパイラのバグにぶつかっただけですか?
そうそう:
$ dmd -v
DMD64 D Compiler v2.053
...