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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:38:10+00:00 2026-05-15T05:38:10+00:00

void main() { int xyz = 123; // original value { // code block

  • 0
void main()
{
    int xyz = 123; // original value
    { // code block starts
        xyz++;
        if(xyz < 1000)
            xyz = 1;
    } // code block ends
    int original_value = xyz; // should be 123
}

void main()
{
    int xyz = 123; // original value
    MACRO_NAME(xyz = 123) // the macro takes the code code that should be executed at the end of the block.
    { // code block starts
        xyz++;
        if(xyz < 1000)
            xyz = 1;
    } // code block ends << how to make the macro execute the "xyz = 123" statement?
    int original_value = xyz; // should be 123
}

Only the first main() works.
I think the comments explain the issue.

It doesn’t need to be a macro but to me it just sounds like a classical “macro-needed” case.

By the way, there’s the BOOST_FOREACH macro/library and I think it does the exact same thing I’m trying to achieve but it’s too complex for me to find the essence of what I need.
From its introductory manual page, an example:

#include <string>
#include <iostream>
#include <boost/foreach.hpp>

int main()
{
    std::string hello( "Hello, world!" );

    BOOST_FOREACH( char ch, hello )
    {
        std::cout << ch;
    }

    return 0;
}
  • 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-15T05:38:10+00:00Added an answer on May 15, 2026 at 5:38 am

    The cleanest way to do this is probably to use an RAII container to reset the value:

    // Assumes T's assignment does not throw
    template <typename T> struct ResetValue
    {
        ResetValue(T& o, T v) : object_(o), value_(v) { }
        ~ResetValue() { object_ = value_; }
    
        T& object_;
        T value_;
    };
    

    used as:

    {
        ResetValue<int> resetter(xyz, 123);
        // ...
    }
    

    When the block ends, the destructor will be called, resetting the object to the specified value.

    If you really want to use a macro, as long as it is a relatively simple expression, you can do this using a for-block:

    for (bool b = false; b == false; b = true, (xyz = 123))
    {
        // ...
    }
    

    which can be turned into a macro:

    #define DO_AFTER_BLOCK(expr) \
        for (bool DO_AFTER_BLOCK_FLAG = false; \
             DO_AFTER_BLOCK_FLAG == false; \
             DO_AFTER_BLOCK_FLAG = true, (expr))
    

    used as:

    DO_AFTER_BLOCK(xyz = 123)
    {
        // ...
    }
    

    I don’t really think the macro approach is a good idea; I’d probably find it confusing were I to see this in production source code.

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

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found the best way to do this was to… May 16, 2026 at 11:14 pm
  • Editorial Team
    Editorial Team added an answer you could also hide the wrapper in the content like… May 16, 2026 at 11:14 pm
  • Editorial Team
    Editorial Team added an answer It's a Visual Studio extension you have to install through… May 16, 2026 at 11:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

#include stdio.h #include conio.h void swap(int *x,int *y); void main() { int a=10,b=20; swap(a,b);
Below are 2 programs First #include<stdio.h> void main() { int a[5]={1,2,3,4,5}; int *p; p=&a;
#include<stdio.h> #include<conio.h> #define ABC 20 #define XYZ 10 #define XXX ABC - XYZ void
In C++ C: Output: 1610612736 #include <math.h> #include <stdio.h> int main(int argc, char** argv)
As part of answering another question, I came across a piece of code like
My questions aims at the behaviour of setjmp/longjmp concerning local variables. Example code: jmp_buf
I have a simple problem that says: A password for xyz corporation is supposed
In the following program, loop is iterating 1000 times, and I am writing all
I have the following C# code fragment: using System; class count { public static
I observed that Outer classes can access inner classes private instance variables. How is

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.