How could one determine the number and type of the class constructor’s parameters?
To do that for a member function is just a piece of cake:
template <class T, typename P0, typename P1, typename P2, typename P3>
void BindNativeMethod( void (T::*MethodPtr)(P0, P1, P2, P3) )
{
// we've got 4 params
// use them this way:
std::vector<int> Params;
Params.push_back( TypeToInt<P0>() );
Params.push_back( TypeToInt<P1>() );
Params.push_back( TypeToInt<P2>() );
Params.push_back( TypeToInt<P3>() );
}
template <class T, typename P0, typename P1, typename P2, typename P3, typename P4>
void BindNativeMethod( void (T::*MethodPtr)(P0, P1, P2, P3, P4) )
{
// we've got 5 params
// use them this way:
std::vector<int> Params;
Params.push_back( TypeToInt<P0>() );
Params.push_back( TypeToInt<P1>() );
Params.push_back( TypeToInt<P2>() );
Params.push_back( TypeToInt<P3>() );
Params.push_back( TypeToInt<P4>() );
}
and so on for other members.
But what to do with the class constructors? Is there any way to find out the type of their arguments? Maybe there’s a fundamentally different approach to solve this because it’s even impossible to take the address of the constructor?
Edit: I have a C++ preprocessor that scans all source files and has the database of all classes, methods, ctors and their exact prototypes. I need to generate some stubs based on this.
If I understand your requirement correctly, you want a function that you can take the address of that tells you the types of the parameters to the constructor or constructors.
Implementing the
fake_ctorto actually invoke thector(even thoughfake_ctoritself will never be called) provides a level of compile time sanity. IfFoochanges one of the constructors without regenerating theBindClassCtorcalls, it will likely result in a compile time error.Edit: As a bonus, I simplified parameter binding using templates with variadic arguments. First, the
BindParamstemplate:Now,
fake_ctoris now a collection of classes, so that each can be instantiated by a variadic parameter list:And now the binder function is simply this:
Below is an illustration of the constructor argument bindings for
Barthat has four constructors.