views:

140

answers:

3

Suppose I have the following code:

int f(int, int);

int main()
{
    SomeFunc(boost::bind(f, 1, 2));
}

From the SomeFunc() function, is it possible to access the arguments held by the bound type? Something like this (pseudo code):

// Obvious syntax issues...
void SomeFunc(boost::bind& functor)
{
    if(functor.function == &f)
    {
        if(functor.argument1 == 1)
            DoSomething();
    }
}

Can I pull this information out of the boost::bind type?

+2  A: 

The real question is why would you want to do that?
I suspect you can't but the fact that you are trying is a bit worrying.

Martin York
A: 

No, you cannot do that with boost::bind.

boost::bind just generates a sort of functor object where all details are hidden. Than you construct boost::function or boost::signal with it and the only thing you can do: execute. You even cannot compare boost::function objects.

Anyway, it is not clear that the problem you are solving. Such approach looks awkward to me. Are you sure you really need that?

bocco
+4  A: 

boost::bind is a templated function, not a type. The real type returned by that function is some kind of functor of an unspecified type. As a matter of fact, it probably returns many different unspecified types depending on what the arguments to the boost::bind function are.

As the type is unspecified and the library only states that is CopyConstructible, that implements operator() with the appropriate number and type of arguments (one for each placeholder, types deduced from the bound method/function) and that it offers an inner type *result_type* that is the same as the return type of that operator().

The interface of those unspecified classes is, well, unspecified. It will probably not offer accessors to the arguments, and even if it does, and you get inside knowledge from studying the internals of the library, you risk having your code break with upgrades to the library (the implementor is free to change the type and all the interface that is not publicly documented).

The whole library is built around the fact that you do not really care about what the arguments are or even if any argument is defined or only placeholders are used, you only care that the resulting object will be callable with a given interface.

So no, you cannot.

David Rodríguez - dribeas