Boost logo

Boost Users :

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


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 {
    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) {
        } else if(i == 2) {
        } else {
    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;
   s(1, b);

int main() {

In Christ,
Steven Watanabe

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at