On 1/16/07, Peter Dimov
Chris Uzdavinis wrote:
Can anyone tell me how to determine the arity of an arbitrary bind object? I have a generic function that receives a bind object, but so far I've been unable to detect at compile time how many arguments it takes. Am I overlooking something obvious?
No, there's no obvious way to determine the minimum arity of a bind object (its maximum arity is 9). What is the specific scenario? Does it have to be at compile time?
Hi, thanks for the feedback. I'll try to explain the situation
without going overboard.
I have several derived classes from an abstract base, each holding a
boost::function object with a slightly different signature, namely,
arity0 through arity N. When I instantiate one of these classes I
pass an object that matches its signature in arity.
To simplify creation of these objects, I have some overloaded
functions, make_func(), which take a function (or member function ptr
+ object address) and instantiate the correct derived class to wrap
it.
My goal is to make the generator functions work when given a boost
bind object. That is, I'd like to write a make_func() overload that
accepts a template type T, and when T is a bind object, it uses some
sort of MPL introspection to decide which derived class is required to
hold this bind object, instanties it, and returns the pointer, just
like all the other make_func generator functions do.
Here's some stripped down and simplified code. The very last function
is the one I don't know how to write. If only bind objects had a
"min_arity" member constant.... :)
class Function
{
public:
typedef boost::shared_ptr<Function> Shptr;
virtual ~Function() { }
virtual std::string call() {error(0);}
virtual std::string call(std::string const &) {error(1);}
virtual int arity() const = 0;
private:
void error(int num_args)
{
std::ostringstream msg;
msg << "Arity Error: expected " << arity()
<< "args, received " << num_args;
throw std::runtime_error(msg.str());
}
};
typedef boost::functionstd::string()
Arity0_Func;
typedef boost::function