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 回答