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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:28:03+00:00 2026-06-03T21:28:03+00:00

I’m trying to utilize the preprocessor in C++ in a way which would ease

  • 0

I’m trying to utilize the preprocessor in C++ in a way which would ease my development progress enormously! I have a pretty simple issue, I work with a C API library whilst I use regular C++ classes. This library is based on callbacks/events, which I only can supply functions (not methods) to. Because of this I have a repetitive pattern of declaring a static and non-static function for each event:

public: // Here is the static method which is required
    static inline Vector StaticEventClickLeft(Vector vec) { return globalClass->EventClickLeft(vec); }
private: // And here is the method (i.e non-static)
    Vector EventClickLeft(Vector vec);

I want to create a macro which defines both these in a single line. It would decrease the size of my header by ten times at least! Here is my closest attempt (but far from enough):

#define DECL_EVENT(func, ret, ...) \
    public: static inline ret S ## Event ## func(__VA_ARGS__) { return globalClass->Event ## func(__VA_ARGS__); } \
    private: ret Event ## func(__VA_ARGS__);

If I use this macro like this DECL_EVENT(ClickLeft, Vector, Vector vec). This will be the output:

public: static inline Vector SEventClickLeft(Vector vec) { return globalClass->EventClickLeft(Vector vec); }
private: Vector EventClickLeft(Vector vec);

You can clearly see the problem. The static function calls the method and supplies the type of the argument as well as the name. Since the type is specified, it results in a compiler error; include/plugin.h:95:2: error: expected primary-expression before ‘TOKEN’ token
.

So how can I solve this? There must be some solution, and I’m sure some macro expert can offer some help!

  • 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-03T21:28:05+00:00Added an answer on June 3, 2026 at 9:28 pm

    First, place parenthesis around your types, so the preprocessor can parse it. So you will call DECL_EVENT like this:

    DECL_EVENT(ClickLeft, Vector, (Vector) vec)
    

    Here are some macros that will retrieve the type and strip off the type(you will want to namespace them, I am leaving off the namespace just for demonstration):

    #define EAT(...)
    #define REM(...) __VA_ARGS__
    #define STRIP(x) EAT x
    #define PAIR(x) REM x
    

    These macros work like this. When you write STRIP((Vector) vec) it will expand to vec. And when you write PAIR((Vector) vec) it will expand to Vector vec. Now next, you will want to do is to apply these macros to each argument that is passed in, so here is a simple APPLY macro that will let you do that for up to 8 arguments:

    /* This counts the number of args */
    #define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
    #define NARGS(...) NARGS_SEQ(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
    
    /* This will let macros expand before concating them */
    #define PRIMITIVE_CAT(x, y) x ## y
    #define CAT(x, y) PRIMITIVE_CAT(x, y)
    
    /* This will call a macro on each argument passed in */
    #define APPLY(macro, ...) CAT(APPLY_, NARGS(__VA_ARGS__))(macro, __VA_ARGS__)
    #define APPLY_1(m, x1) m(x1)
    #define APPLY_2(m, x1, x2) m(x1), m(x2)
    #define APPLY_3(m, x1, x2, x3) m(x1), m(x2), m(x3)
    #define APPLY_4(m, x1, x2, x3, x4) m(x1), m(x2), m(x3), m(x4)
    #define APPLY_5(m, x1, x2, x3, x4, x5) m(x1), m(x2), m(x3), m(x4), m(x5)
    #define APPLY_6(m, x1, x2, x3, x4, x5, x6) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6)
    #define APPLY_7(m, x1, x2, x3, x4, x5, x6, x7) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6), m(x7)
    #define APPLY_8(m, x1, x2, x3, x4, x5, x6, x7, x8) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6), m(x7), m(x8)
    

    Now heres how you can write your DECL_EVENT macro:

    #define DECL_EVENT(func, ret, ...) \
    public: static inline ret S ## Event ## func(APPLY(PAIR, __VA_ARGS__)) { return globalClass->Event ## func(APPLY(STRIP, __VA_ARGS__)); } \
    private: ret Event ## func(APPLY(PAIR, __VA_ARGS__));
    

    Note: This probably won’t work in MSVC, since they have a buggy preprocessor(although there are workarounds).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

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.