我希望这段代码能够编译通过,但事实并非如此。
如何避免出现此错误消息?
ReturnType tryMe(ReturnType)() {
static if (is(ReturnType == int)) {
return 42;
} else static if (is(ReturnType == string)) {
return "Hello!";
} else {
assert(0);
}
}
unittest {
string r = tryMe();
assert(r == "Hello!");
int v = tryMe();
assert (v == 42);
}
如何避免出现此错误消息?
Error: template app.tryMe cannot deduce function from argument types !()(), candidates are:
app.tryMe(ReturnType)()
如果我“重构”我的函数,使它通过传入的引用返回结果,代码就能够编译。但是这会让函数的API非常丑陋。