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

  • SEARCH
  • Home
  • 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 6158109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:54:05+00:00 2026-05-23T20:54:05+00:00

How do you write a macro with variable number of arguments to define a

  • 0

How do you write a macro with variable number of arguments to define a function? Suppose that we define the class class1 with 2 parameters and class class2 with three parameters.

class class1 {
public:
   int arg1;
   int arg2;
   class1(int x1, int x2): arg1(x1), arg2(x2) {}
};
class class2 {
public:
   int arg1;
   int arg2;
   int arg3;
   class1(int x1, int x2, int x3): arg1(x1), arg2(x2), arg3(x3) {}
};

For each class that I define or even classes that have been defined before I want to write the following:

template<> inline void writeInfo<class1>(const class1& obj, FILE* fp) {
    writeAmount(2, fp);
    writeName("arg1", fp);
    writeInfo(obj.arg1, fp);
    writeName("arg2", fp);
    writeInfo(obj.arg2, fp);
}
template<> inline void writeInfo<class2>(const class2& obj, FILE* fp) {
    writeAmount(3, fp);
    writeName("arg1", fp);
    writeInfo(obj.arg1, fp);
    writeName("arg2", fp);
    writeInfo(obj.arg2, fp);
    writeName("arg3", fp);
    writeInfo(obj.arg3, fp);
}

We do not need to care about the definitions of writeAmount, writeName or writeInfo. What I would like to do is write something like:

MACROWRITEINFO(class1, 2, arg1, arg2);
MACROWRITEINFO(class2, 3, arg1, arg2, arg3);

Is it possible to create such macro so that it can expand to the above template definitions? I’ve read in a lot of places that macros are evil, but in this case I believe that they are very helpful since they’ll reduce the amount of code I type and thus the amount of typos I’ll make during the creation of the template functions.

  • 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-05-23T20:54:06+00:00Added an answer on May 23, 2026 at 8:54 pm

    First of all you should improve your formatting/code. Your code lacks “class” keywords and semicolons after classes definitions – when you post a snippet make sure it’s proper code, because some people (i.e. me) will try to compile it.

    Second of all, dont use function template specialization. If macros are evil, then they must be satan incarnation. Just stick to the good old overloads. See here for details.

    And at least – an answer. You could mess around with variadic macros if all args were of the same type – for example, you could create an array inside writeInfo function and iterate over elements. Since it’s cleary not the case here you can define many variants of MACROWRITEINFO macro for different number of parameteres, using some common blocks to reduce code repetition. For example:

    #define MACROWRITEINFO_BEGIN(type, amount)  \
    void writeInfo(const type& obj, FILE* fp)   \
    {                                           \
        writeAmount(amount, fp);
    
    #define MACROWRITEINFO_NAMEINFO(name)       \
        writeName(#name, fp);                   \
        writeInfo(obj.##name, fp);
    
    #define MACROWRITEINFO_END()                \
    }
    

    Using those you can now define variants based on number of arguments.

    #define MACROWRITEINFO1(type, arg1) \
        MACROWRITEINFO_BEGIN(type, 1)   \
        MACROWRITEINFO_NAMEINFO(arg1)   \
        MACROWRITEINFO_END()
    
    #define MACROWRITEINFO2(type, arg1, arg2) \
        MACROWRITEINFO_BEGIN(type, 2)   \
        MACROWRITEINFO_NAMEINFO(arg1)   \
        MACROWRITEINFO_NAMEINFO(arg2)   \
        MACROWRITEINFO_END()
    

    And so on…

    EDIT:
    Well I guess it is possible to use variadic macros here. Take at look at this SO question. It’s pure madness, but you should be able to achieve what you want.

    EDIT:
    My idea was to expand variadic arguments into array then iterate over them; if they were of the same type, let’s say int, you could write:

    #define VAARGSSAMPLE(...) \
        int args[] = { __VA_ARGS__ }; \
        for (int i = 0; i < sizeof(args)/sizeof(int); ++i) \
        { \
            printf("%d\n", args[i]); \
        }
    
    VAARGSSAMPLE(1, 5, 666);
    

    So if all your variables were of the same type you could put them in an array. But they are not, so it won’t do. If you really, really want to stick to variadic arguments go to my first edit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I allow user to write his own variable calculation macro using a common
I try to write a macro in clojure that sets up a namespace and
I'm trying to write a macro that toggles between release/debug solution configurations in Visual
Let's say that for some reason you need to write a macro: MACRO(X,Y) .
I've been attempting to write a Lisp macro that would perfom the equivalent of
I know that it's possible to write a register macro that will map their
I want to write a macro that compares two times that is available in
Is it possible to write a Macro (using token concatenation) that returns format for
I am trying to write a macro that will be attached to a series
I'm trying to write a macro in Lisp that re-implements let using itself. This

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.