Consider the next sample of the code:
template <typename... TArgs>
void foo(std::function<void(TArgs...)> f) {
}
template <typename... TArgs>
class Class {
public:
static void foo(std::function<void(TArgs...)> f) {
}
};
Why can I do this:
int main() {
// Helper class call
Class<int, int>::foo(
[](int a, int b) {}
);
}
But I get a compile error doing this:
int main() {
// Function call
foo<int, int>(
[](int a, int b) {}
);
}
<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...)>' against
'(lambda at <source>:17:9)'
void foo(std::function<void(TArgs...)> f) {
^
I just want to have a convenient way to use functions such as foo
.
I've tried this:
std::function<void(int, int)> f = [](int a, int b) {
};
foo<int, int>(f); // ok
And it worked. And this is ok. But I want to know if there is any way to use lambdas right in the function call, without creating a local function object.