Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:01:31+00:00 2026-06-08T07:01:31+00:00

How could one determine the number and type of the class constructor’s parameters? To

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:01:33+00:00Added an answer on June 8, 2026 at 7:01 am

    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.

    #include <string>
    #include <new>
    
    template <typename T, typename P0, typename P1>
    T * fake_ctor (T *mem, P0 p0, P1 p1) {
        return mem ? new (mem) T(p0, p1) : 0;
    }
    
    struct Foo {
        Foo (int, int) {}
        Foo (int, std::string) {}
    };
    
    template <class T, typename P0, typename P1>
    void BindClassCtor(T *(*FakeCtor)(T *, P0, P1)) {
        std::vector<int> Params;
        Params.push_back( TypeToInt<P0>() );
        Params.push_back( TypeToInt<P1>() );
    }
    
    // PARSER GENERATED CALLS
    BindClassCtor<Foo, int, int>(&fake_ctor);
    BindClassCtor<Foo, int, std::string>(&fake_ctor);
    

    Implementing the fake_ctor to actually invoke the ctor (even though fake_ctor itself will never be called) provides a level of compile time sanity. If Foo changes one of the constructors without regenerating the BindClassCtor calls, it will likely result in a compile time error.

    Edit: As a bonus, I simplified parameter binding using templates with variadic arguments. First, the BindParams template:

    template <typename... T> struct BindParams;
    
    template <typename T, typename P0, typename... PN>
    struct BindParams<T, P0, PN...> {
        void operator () (std::vector<int> &Params) {
            Params.push_back( TypeToInt<P0>() );
            BindParams<T, PN...>()(Params);
        }
    };
    
    template <typename T>
    struct BindParams<T> {
        void operator () (std::vector<int> &Params) {}
    };
    

    Now, fake_ctor is now a collection of classes, so that each can be instantiated by a variadic parameter list:

    template <typename... T> struct fake_ctor;
    
    template <typename T>
    class fake_ctor<T> {
        static T * x (T *mem) {
            return mem ? new (mem) T() : 0;
        }
        decltype(&x) y;
    public:
        fake_ctor () : y(x) {}
    };
    
    template <typename T, typename P0>
    class fake_ctor<T, P0> {
        static T * x (T *mem, P0 p0) {
            return mem ? new (mem) T(p0) : 0;
        }
        decltype(&x) y;
    public:
        fake_ctor () : y(x) {}
    };
    
    template <typename T, typename P0, typename P1>
    class fake_ctor<T, P0, P1> {
        static T * x (T *mem, P0 p0, P1 p1) {
            return mem ? new (mem) T(p0, p1) : 0;
        }
        decltype(&x) y;
    public:
        fake_ctor () : y(x) {}
    };
    

    And now the binder function is simply this:

    template <typename... T>
    void BindClassCtor (fake_ctor<T...>) {
        std::vector<int> Params;
        BindParams<T...>()(Params);
    }
    

    Below is an illustration of the constructor argument bindings for Bar that has four constructors.

    struct Bar {
        Bar () {}
        Bar (int) {}
        Bar (int, int) {}
        Bar (int, std::string) {}
    };
    
    BindClassCtor(fake_ctor<Bar>());
    BindClassCtor(fake_ctor<Bar, int>());
    BindClassCtor(fake_ctor<Bar, int, int>());
    BindClassCtor(fake_ctor<Bar, int, std::string>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could some one give me pointers to tutorials that explains how to write a
Could one help translating following sed command so it does the same on aix
How could one test whether a set of modules is installed, given the names
Problem in short: How could one implement static if functionality, proposed in c++11, in
When adding a new Entity Framework object to a database, how could one have
could someone one the current list of managed Beans or classes in Java EE
Could any one tell me how to enable SOCKET support in PHP ?
Could some one guide me to create a APS logon token in .Net programmatically?
Could any one please tell me the meaning of ++ with array in the
In the Nav Services world one could specify kNavDontConfirmReplacement as an option to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.