I'm defining a class like this:
class foo {
public:
// I define const and non-const versions of the 'visit' function
// nb. the lambda is passed by reference
virtual void visitWith(std::function<void(foo&)>&);
virtual void visitWith(std::function<void(const foo&)>&) const;
};
foo can have children so the idea is to visit a foo and all it's children recursively.
When I try to use it, eg. like this:
foo f;
f.visitWith([&](const foo&) {
// Do something here
});
I get compiler errors. The compiler can't figure out what to do.
I can make it work it by adding a typecast like this:
foo f;
f.visitWith( (std::function<void(const foo&)>) [&](const foo&) {
// Do something here
});
But that's horrible.
How can I get it to work neatly?
Edit:
This may be a problem with Visual C++, it refuses to compile the code given here:
The VC++ output when I try to compile it is:
Edit2: Nope, Visual C++ is correct, the code is ambiguous. See my solution below...