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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:38:03+00:00 2026-05-21T06:38:03+00:00

In C++, I need to defined a macro. That macro would take as parameter

  • 0

In C++,
I need to defined a macro.
That macro would take as parameter a “block” of code.

Can we safely use several lines of code as parameter of a macro function?

I was asking myself if :

  1. is the following code valid, defined as valid by the standard, as in “cross-platform”?
  2. is there a better way to do the same (I can’t use template function there because I need the context).

#define MY_MACRO( expr ) DOSOMETHING( (expr) ); DOANOTHERTHING( (expr) ); // etc...

int my_function() {
    int o = RandomNumber();
    MY_MACRO( 
        int k = AFunction();
        k++;
        AnotherFunction( k + o ); // here I need to keep the context of the call
    ); 
}

We can’t use functors because we need to have access to the context of the call.
We can’t use lambda (snif) because we use an old compiler that don’t provide it (and we can’t change it).

  • 1 1 Answer
  • 1 View
  • 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-21T06:38:04+00:00Added an answer on May 21, 2026 at 6:38 am

    16.3/9:

    Within the sequence of preprocessing
    tokens making up an invocation of a
    function-like macro, new-line is
    considered a normal white-space
    character.

    So the multi-line macro invocation in general is fine. Of course if DOSOMETHING and DOANOTHERTHING don’t introduce braces for scope, then your particular example will redefine k.

    Edit:

    We can’t use functors because we need
    to have access to the context of the
    call. We can’t use lambda (snif) because we use an old compiler

    The usual way is to capture whichever variables you need in the functor, just like a lambda does. The only thing a lambda can do that a functor can’t is “capture everything” without having to type it out, but whoever writes the lambda can see what variables they use, so that’s just convenience, they could type them all out if they had to. In your example:

    struct MyFunctor {
        int o;
        MyFunctor(int o) : o(o) {}
        void operator()() const {  // probably not void in practice
            int k = AFunction();
            k++;
            AnotherFunction( k + o );
        }
    };
    
    template<typename F>
    void DoThings(const F &f) {
        DOSOMETHING(f());
        DOANOTHERTHING(f());
    }
    
    int my_function() {
        int o = RandomNumber();
        DoBothThings(MyFunctor(o));
    }
    

    You can also capture variables by reference (usually using a pointer as the data member rather than a reference, so that the functor can be copy-assigned).

    If by “context”, you mean for example that the macro argument and/or the macro body might contain a break or goto, and hence needs to be inside the lexical scope of the caller then you can’t use a functor or a lambda. For shame 😉

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

Sidebar

Related Questions

Why calling a user defined function need the owner name when calling a stored
I am wondering if macro definitions can contain space. Let's take for example this
I am trying to create a macro that takes a scope as a parameter.
Is there a #define compiler (nvcc) macro of CUDA which I can use? (Like
I want to define a macro that can be invoked in different places (at
I have a preprocessor macro defined in build settings FOO=BAR That value I want
I need to enumerate all the user defined types created in a SQL Server
I need to replace one of the pre-defined window classes all across Windows. For
I need to call a method I have defined as: -(IBAction)next:(id)sender{ ... } I
I need a way to collapse duplicate (defined in terms of a string field

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.