Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-05-31 23:41:25


AMDG

Olaf Krzikalla wrote:
> is it possible to check a function bound to boost::function, if a
> particular argument is actually needed?

Boost.Function doesn't provide such a utility. I can't see a way to
get this information out of boost::bind using the public interface, either.
visit_each skips placeholders. If there is a way you would need
something like this

#include <boost/function.hpp>
#include <boost/bind.hpp>

struct bar {};

class tSlot {
public:
    template<class T>
    tSlot(const T& t) : uses_placeholder1(false),
uses_placeholder2(false), f(t) {
        // do some magic to find which arguments are used.
    }
    void operator()(int arg1, bar& arg2) const { f(arg1, arg2); }
    bool uses_placeholder(int i) const {
        if(i == 1) {
            return(uses_placeholder1);
        } else if(i == 2) {
            return(uses_placeholder2);
        } else {
            return(false);
        }
    }
private:
    bool uses_placeholder1;
    bool uses_placeholder2;
    boost::function<void(int, bar&)> f;
};

void foo(const tSlot& s);
void barNeeded(int, bar&) {}
void barNotNeeded(int) {}

int n;

void expensive_calculation(bar&) {
    n = 0;
}

void callfoo()
{
   foo(boost::bind(&barNeeded, _1, _2));
   foo(boost::bind(&barNotNeeded, _1));
}

void foo(const tSlot& s)
{
   bar b;
   if(s.uses_placeholder(2))
    expensive_calculation(b);
   s(1, b);
}

int main() {
    callfoo();
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net