为什么我不能使用带有谓词函数的std.algorithm.count

Met*_*eta 2 d phobos

以下代码无法编译:

assert("(((())))()()()()))".count!(c => c.among!('(', ')')) > 0);
Run Code Online (Sandbox Code Playgroud)

带有错误消息:

"Error: template std.algorithm.searching.count cannot deduce function from argument types !((c) => c.among!('(', ')'))(string), candidates are..."
Run Code Online (Sandbox Code Playgroud)

但是[标准库(http://dlang.org/phobos/std_algorithm_searching.html#.count)清楚地表明存在一个count带谓词的重载,计算R谓词返回true的所有元素.那么为什么编译器会在我尝试使用count这种方式时抱怨?

小智 7

assert("(((())))()()()()))".count!(c => c.among!('(', ')') != 0) > 0);

问题是:

  1. 你的lambda返回uint而不是bool(检查文档的返回值among).
  2. 编译器错误没有帮助.

  • 这很奇怪,模板约束专门说`是(typeof(unaryFun!pred(haystack.front)):bool)`.我会认为`是(uint:bool)`会返回true,但我猜不会. (3认同)