【发布时间】:2012-06-21 15:14:26
【问题描述】:
考虑这个模板函数:
template<typename ReturnT>
ReturnT foo(const std::function<ReturnT ()>& fun)
{
return fun();
}
为什么编译器不能从传递的调用签名中推断出ReturnT?
bool bar() { /* ... */ }
foo<bool>(bar); // works
foo(bar); // error: no matching function call
【问题讨论】:
-
one of my previous questions 的答案允许这种语法。我希望这会有所帮助。它专门用于返回类型为 void 的情况。
-
@chris 感谢您的提示 - 我会尽力理解它:)
-
注:与stackoverflow.com/q/7608741/981959 和stackoverflow.com/q/9242234/981959 非常相似,但我认为对于这个特定问题,这里的答案更清楚。
标签: c++ templates std-function template-argument-deduction