【问题标题】:How to check if a std::function is set and valid?如何检查 std::function 是否已设置且有效?
【发布时间】:2021-03-29 09:43:12
【问题描述】:

我有一个std::function,设置如下:

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

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

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

在调用m_the_listener() 之前检查if (m_the_listener) 是否足够?

【问题讨论】:

    标签: c++ c++11 c++14 std-function


    【解决方案1】:

    This 确实声明验证条件中的std::function 实例应该足以验证它是否具有有效的可调用目标。所以是的,if (m_the_listener) 应该足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 2013-09-17
      • 1970-01-01
      • 2020-03-12
      • 2014-06-15
      • 1970-01-01
      • 2020-07-12
      相关资源
      最近更新 更多