5

次のコードを検討してください。

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
...
4

1 に答える 1

5

バグかどうかはわかりませんが、タイプを指定したり、引数の順序を変更したりする必要がない回避策を次に示します。consumeの署名を次のように変更します。

S consume (S, U) (ref S data, Regex!U rg) if (is(U == Unqual!(typeof(S.init[0]))))
于 2011-06-21T18:00:01.150 に答える