D功能模板和类型推断

时间:2011-06-21 15:08:44

标签: d type-inference function-templates

请考虑以下代码:

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

1 个答案:

答案 0 :(得分:5)

我不能说这是不是一个错误,但这是一个解决方法,它不会强制您指定类型或更改参数的顺序。将consume的签名更改为:

S consume (S, U) (ref S data, Regex!U rg) if (is(U == Unqual!(typeof(S.init[0]))))
相关问题