3

Having a non-empty boost::function, how to make it empty (so when you call .empty() on it you'll get true)?

jrok
  • 54,456
  • 9
  • 109
  • 141
DuckQueen
  • 772
  • 10
  • 62
  • 134

2 Answers2

4

Simply assign it NULL or a default constructed boost::function (which are empty by default):

#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();
}

Output: 011

jrok
  • 54,456
  • 9
  • 109
  • 141
4

f.clear() will do the trick. Using the example above

#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();
}

will yield the same result.

Lou
  • 1,955
  • 14
  • 16