简体   繁体   English

如何检查 std::function 是否已设置且有效?

[英]How to check if a std::function is set and valid?

I have an std::function which is being set like below:我有一个std::function设置如下:

typedef std::function<void(bool some_state)> TheListener;
void ListenTo(TheListener the_listener) {
    m_the_listener = the_listener;
}

How do I check if the std::function is set?如何检查是否设置了std::function

private:
    void SomePrivateMethod() {
        // How do I check here that m_the_listener is set by calling ListenTo ?
        m_the_listener();
    }

Is it enough to check if (m_the_listener) before calling m_the_listener() ?在调用m_the_listener() if (m_the_listener)是否足够?

This does state that verifying the std::function instance in a condition should be sufficient to verify if it has a valid callable target. 确实 state 在条件下验证std::function实例应该足以验证它是否具有有效的可调用目标。 So yes, if (m_the_listener) should be enough.所以是的, if (m_the_listener)就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM