Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear boost function?

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

like image 391
DuckQueen Avatar asked Aug 03 '26 23:08

DuckQueen


2 Answers

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

like image 118
jrok Avatar answered Aug 05 '26 13:08

jrok


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.

like image 42
Lou Avatar answered Aug 05 '26 13:08

Lou