【发布时间】:2015-08-13 07:30:56
【问题描述】:
我希望这段代码能够编译,但事实并非如此。
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很丑。
【问题讨论】:
标签: templates d type-inference