The type of a lambda is inexpressible so they can only be handled with auto or decltype. I think that’s reasonable.
The type of a lambda is inexpressible so they can only be handled with auto or decltype. I think that’s reasonable.
Traditional callbacks can also be used in this case, but what is a callback lambda but private state? And a stateful object rather than a lambda function is easier to manipulate, say by inspecting in the debugger or perhaps by providing a generic function for things like printing the entry.
Another case is where you have some hairball third party or legacy library that you want to be able to use in your modern code; you can often simply make it an opaque private object rather than making a complex visitor for it.
class Token {
virtual ~Token();
protected:
friend class MyClass;
...
/* all your state */
};
> what is a callback lambda but private state?You can (and do) program to the callback's interface -- say you ask for an std::function<void()>, and the user gives you that. What is behind the function (private state or not) is not your concern, you simply perform an public operation (calling it) on a public interface (i.e., via std::function).
Going back to the original point i was making with this example, I still think this was probably not a capability consciously designed in the language.