[C++-sig] Check number of arguments of a function object ?

Joseph Lisee jlisee at gmail.com
Tue Feb 26 21:42:47 CET 2008


Marcus,

This is not tested but this should do what you want, python example first:
>>> def func(a):
...     pass
... 
>>> func.func_code.co_argcount
1
>>> def func(a,b):
...     pass
... 
>>> func.func_code.co_argcount
2

Some (untested) code to do this in Boost.Python:

bool hasTwoArgs(bp::object callable)
{
    try
    {
        int argCount = bp::extract<int>(callable["func_code"]["co_argcount"]);
        if (2 != argCount)
            return false;
    } catch (bp::error_already_set e) {
        // Catch mallformed function objects
        return false;
    }
    return true;
}

-Joseph Lisee




More information about the Cplusplus-sig mailing list