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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:25:09+00:00 2026-05-22T19:25:09+00:00

I am programmatically generating bunch of functors, in order to keep the generated code

  • 0

I am programmatically generating bunch of functors, in order to keep the generated code more readable I am trying to come up with a macro that will expand line the following,

SET_STATE(FunctorA,a,b);

ref a;
ref b;
FunctorA(ref a, ref b){
   this->a = a;
   this->b = b;
}

Basically it will expand to the first arguments constructor. Variadic part is the number of arguments to the constructor. is it possible to loop inside the macro and generate this code during preprocessing even though it does not make sense for this particular case but I have some functors that have 20 or so variables that they have access to it will cleanup my generated code a lot.

All arguments will be of the same type, only names will differ.

  • 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-22T19:25:10+00:00Added an answer on May 22, 2026 at 7:25 pm

    Using the tricked found in this link http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/ to count the number of arguments and using some really ugly macro I can generate the output you wanted.

    I tested it using gcc (gcc -E test.cpp) and it works, It’s not portable.

    Code:

    #define VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
    #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
    #define SET_STATEGENERATE(name, count, ...)             \
            dec ## count (__VA_ARGS__)                      \
            name(ref ## count (__VA_ARGS__)) {              \
                con ## count (__VA_ARGS__)                  \
            }
    #define SET_STATEP(name, count, ...) SET_STATEGENERATE(name, count, __VA_ARGS__) 
    #define SET_STATE(name, ...) SET_STATEP(name, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
    /* args */
    #define dec1(a) ref a;
    #define dec2(a,b) dec1(a) ref b;
    #define dec3(a,b,c) dec2(a, b) ref c;
    #define dec4(a,b,c,d) dec3(a,b,c) ref d;
    #define dec5(a,b,c,d,e) dec4(a,b,c,d) ref e;
    #define dec6(a,b,c,d,e,f) dec5(a,b,c,d,e) ref f;
    #define dec7(a,b,c,d,e,f,g) dec6(a,b,c,d,e,f)ref g;
    #define dec8(a,b,c,d,e,f,g,h) dec7(a,b,c,d,e,f,g) ref h;
    #define ref1(a) ref a
    #define ref2(a,b) ref1(a), ref b
    #define ref3(a,b,c) ref2(a,b), ref c
    #define ref4(a,b,c,d) ref3(a,b,c), ref d
    #define ref5(a,b,c,d,e) ref4(a,b,c,d), ref e
    #define ref6(a,b,c,d,e,f) ref5(a,b,c,d,e), ref f
    #define ref7(a,b,c,d,e,f,g) ref6(a,b,c,d,e,f), ref g
    #define ref8(a,b,c,d,e,f,g, h) ref7(a,b,c,d,e,f,g), ref h
    #define con1(a) this->a = a;
    #define con2(a,b) con1(a) this->b = b;
    #define con3(a,b,c) con2(a,b) this->c = c;
    #define con4(a,b,c,d) con3(a,b,c) this->d = d;
    #define con5(a,b,c,d,e) con4(a,b,c,d) this->e = e;
    #define con6(a,b,c,d,e,f) con5(a,b,c,d,e) this->f = f;
    #define con7(a,b,c,d,e,f,g) con6(a,b,c,d,e,f) this->g = g;
    #define con8(a,b,c,d,e,f,g,h) con7(a,b,c,d,e,f,g) this->h = h;
    

    So the following:

    /* 2 args */
    SET_STATE(FunctorAA, foo, bar)
    /* 3 args */
    SET_STATE(FunctorBB, foo, bar, baz)
    /* 4 args */    
    SET_STATE(FunctorCC, foo, bar, baz, qux)
    

    will produce:

    ref foo; ref bar; FunctorAA(ref foo, ref bar) { this->foo = foo; this->bar = bar; }
    ref foo; ref bar; ref baz; FunctorBB(ref foo, ref bar, ref baz) { this->foo = foo; this->bar = bar; this->baz = baz; }
    ref foo; ref bar; ref baz; ref qux; FunctorCC(ref foo, ref bar, ref baz, ref qux) { this->foo = foo; this->bar = bar; this->baz = baz; this->qux = qux; }
    

    Note: you can continue the number of arguments following the obvious pattern.

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

Sidebar

Related Questions

I am programmatically generating HTML and want to link to a CSS file that
Using jQuery I'm programmatically generating a bunch of div 's like this: <div class=mydivclass
My application is programmatically generating some html code like this: <div class=container> <div class=class1>
I'm running into an issue with Wicket generating markup for elements that programmatically need
I'm having an issue when trying to programmatically update a DateTime field that already
I am generating a report with that will have a 7 columns where the
I'm programmatically creating an edmx file as part of our code generation process and
I'm programmatically adding ToolStripButton items to a context menu. That part is easy. this.tsmiDelete.DropDownItems.Add(The
I have written a Google App Engine application that programatically generates a bunch of
I am generating audio programmatically. I hear gaps of silence between my buffers. When

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.