【问题标题】:How to clear boost function?如何清除升压功能?
【发布时间】:2013-04-02 09:44:35
【问题描述】:

有一个非空的boost::function,如何让它为空(所以当你调用.empty()时你会得到true)?

【问题讨论】:

    标签: c++ boost boost-function


    【解决方案1】:

    只需分配它NULL 或默认构造的boost::function(默认为空):

    #include <boost/function.hpp>
    #include <iostream>
    
    int foo(int) { return 42; }
    
    int main()
    {
        boost::function<int(int)> f = foo;
        std::cout << f.empty();
    
        f = NULL;
        std::cout << f.empty();
    
        f = boost::function<int(int)>();
        std::cout << f.empty();
    }
    

    输出:011

    【讨论】:

      【解决方案2】:

      f.clear() 可以解决问题。使用上面的例子

      #include <boost/function.hpp>
      #include <iostream>
      
      int foo(int) { return 42; }
      
      int main()
      {
          boost::function<int(int)> f = foo;
          std::cout << f.empty();
      
          f.clear();
          std::cout << f.empty();
      
          f = boost::function<int(int)>();
          std::cout << f.empty();
      }
      

      将产生相同的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2011-09-16
        相关资源
        最近更新 更多