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 9306909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:16:32+00:00 2026-06-19T00:16:32+00:00

I want to write a C++ wrapper for a bunch of functions which are

  • 0

I want to write a C++ wrapper for a bunch of functions which are all similar in what they do. But they all have a different number of arguments. For example (assume typeA, typeB, etc. are different typedef’d types):

typeA func1 (typeB b, typeC c);

typeA func2 (typeB b, typeD d, typeE e);

These are the functions I want to write a wrapper for (notice that both have return type typeA and first argument of type typeB). So I intend to create a general wrapper class and store a list of arguments in the constructor. And I want to be able to call a function which looks like ‘func’ in the following:

class wrapper {
  public:
    wrapper (/*args - a list of arguments, including 
               a pointer to the actual function, and 
               the remaining arguments depending 
               on the function. */);
    void func (typeB b, typeA &a);
}

So, I want to write func in such a way that it calls whatever function (either func1 or func2) with the arguments passed into the constructor.

I want to know if there is a way I can treat func1 or func2 as though they have a variable number of arguments. Or at least, if there is a way in C++ in which I can feed in a function and a list of arguments and get eval (function, argument_list).

The alternative is writing a wrapper for every single function, which I want to avoid but am not averse to. (Also, I don’t mind if a solution involves me not using a wrapper class but a wrapper function)

  • 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-19T00:16:33+00:00Added an answer on June 19, 2026 at 12:16 am

    Here is something I have done in C, to wrap around any function. I hope it will help/inspire you.
    The main restriction is that you can wrap only around user defined functions, since you will have to modify the signature of the function.

    My solution uses the va_arg structure.

    #include <stdarg.h>
    void * super_wrapper( void * (*fn)(), void * ret, int n, ...)
    {
      va_list = my_list;
      va_start(my_list, n);
      ret = (*fn)(ret, n, my_list);
      va_end(my_list);
      return ret
    }
    

    The signature of the wrapper takes the pointer to the function you want to execute, a pointer to give to the function to retrieve your return value, the number of arguments of youe call to the function, and then the arguments of the function you want to call.

    The va_list is a list of arguments.
    You initialize it with va_start. The first argument of va_start is the va_list you want to initialize. The second argument is the last known parameter of the current function (in this example, it’s ‘n’).

    Now, instead of the ellipsis in your user defined function, you will put an argument va_list.

    Two examples I had:

    double sum (int n, ...)
    {
      int i;
      double res = 0;
      double tmp;
      va_list = my_list;
      va_start(my_list, n);
      for(i=0; i<n; i++)
      {
        tmp = va_arg(my_list, double);
        res += tmp;
      }
      va_end(my_list);
      return res;
    }
    
    void my_print(int n, ...)
    {
      int i;
      va_list my_list;
      va_start(my_list, n);
      char * tmp;
      for(i=0; i<n; i++)
      {
        tmp = va_arg(my_list, char *);
        printf("%s ", tmp);
      }
      printf("\n");
      va_end(my_list);
    }
    

    I managed to use the same wrapper to call this two different functions with some modifications in the signature and in the function bodies.
    The thing is that in arguments and in returns, I only give pointers. So I have to dereference the pointers.

    void * sum (void * ret, int n, va_list my_list)
    {
      int i;
      double res = 0;
      double *tmp;
      for(i=0; i<n; i++)
      {
        tmp = va_arg(my_list, double);
        res += *tmp;
      }
      ret = &res;
      return ret;
    }
    
    void* my_print(int n, ...)
    {
      int i;
      char ** tmp;
      for(i=0; i<n; i++)
      {
        tmp = va_arg(my_list, char **);
        printf("%s ", *tmp);
      }
      printf("\n");
      return ret;
    }
    

    Now I am able to use the same wrapper function in my main to call these two functions:

    void main()
    {
      double two = 2.0;
      double three = 3.0;
      double fifteen = 15.0;
    
      char *I, *am, *a, *great, *wrapper;
      I = malloc(sizeof(char) * 2);
      am = malloc(sizeof(char) * 3);
      a = malloc(sizeof(char)*2);
      great = malloc(sizeof(char) * 6;
      wrapper = malloc(sizeof(char) * 8);
    
      I = strcpy(I, "I\0");
      am = strcpy(am, "am\0");
      a = strcpy(a,"a\0");
      great = strcpy(great, "great\0");
      wrapper = strcpy(wrapper, "wrapper\0");
    
      void * ret;
    
      printf("Sum = %f\n", *( (double*)super_wrapper(sum, ret, 3, &two, &three, &fifteen) ) );
      super_wrapper(my_print, ret, 5, &I, &am, &a, &great, &wrapper);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a wrapper bash script, and to pass all arguments to
I want to write a DBI wrapper, provide select/insert/update/delete, and users can choose which
I want to write a simple wrapper around another class. A small example: class
I went through quite a number of websites, but everywhere they have given how
I want to write a wrapper to a plugin's function, but it uses varargs
I want to write a database wrapper that can operate different types of databases
I want write a simple query which will fetch data from a table (which
I want write a little code analyzer which parses nested structures and translates into
I have a bunch of classes that all contain a Shared ReadOnly Dictionary. If
I want write a ruby wrapper for a existing C library (.so files). Can

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.