Can someone explain why the lambda functions in the commented-out lines will not compile?
#include <iostream>
struct Object {
int num;
void foo();
template <typename... Args>
void fooHelper (int(*f)(Args...), Args... args) {std::cout << (*f)(args...);}
};
int sum (int x, int y) {return x + y;}
void Object::foo() {
fooHelper (sum, 3, 4);
// fooHelper ([](int i, int j)->int {return i + j;}, 3, 4);
// fooHelper ([this](int i, int j)->int {return i + j + num;}, 3, 4);
// Object o{8}; fooHelper ([this, o](int i, int j)->int {return i + j + num + o.num;}, 3, 4);
}
int main() {
Object o{5};
o.foo(); // 7
}
But fooHelper (sum, 3, 4); does compile. How to make the three lambda functions work inside Object::foo()?
Aucun commentaire:
Enregistrer un commentaire