If I have this class
class TextBox : public Widget {
public:
void addKeyPressHandler(std::function<void (std::string)>);
private:
std::function<void (std::string)> keyPressHandler;
};
How do I know if the keyPressHandler has been set or not? Can I initialize it to NULL and then test against that?
std::functionallows you to test whether it holds a callable target via itsoperator bool().For example:
…will call the function only if it has a valid target. Meaning it has been assigned a function pointer, or anything else callable since it was initialized.