考虑下一个代码示例:
template <typename... TArgs>
void foo(std::function<void(TArgs...)> f) {
}
template <typename... TArgs>
class Class {
public:
static void foo(std::function<void(TArgs...)> f) {
}
};
Run Code Online (Sandbox Code Playgroud)
为什么我可以这样做:
int main() {
// Helper class call
Class<int, int>::foo(
[](int a, int b) {}
);
}
Run Code Online (Sandbox Code Playgroud)
但这样做时出现编译错误:
int main() {
// Function call
foo<int, int>(
[](int a, int b) {}
);
}
Run Code Online (Sandbox Code Playgroud)
<source>:16:5: error: no matching function for call to 'foo'
foo<int, int>(
^~~~~~~~~~~~~
<source>:4:6: note: candidate template ignored: could not match
'std::function<void (int, int, TArgs...)>' …Run Code Online (Sandbox Code Playgroud)