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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:54:13+00:00 2026-06-16T08:54:13+00:00

I have always asked this but I have never received a really good answer;

  • 0

I have always asked this but I have never received a really good answer; I think that almost any programmer before even writing the first “Hello World” had encountered a phrase like “macro should never be used”, “macro are evil” and so on, my question is: why? With the new C++11 is there a real alternative after so many years?

The easy part is about macros like #pragma, that are platform specific and compiler specific, and most of the time they have serious flaws like #pragma once that is error prone in at least 2 important situation: same name in different paths and with some network setups and filesystems.

But in general, what about macros and alternatives to their usage?

  • 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-16T08:54:14+00:00Added an answer on June 16, 2026 at 8:54 am

    Macros are just like any other tool – a hammer used in a murder is not evil because it’s a hammer. It is evil in the way the person uses it in that way. If you want to hammer in nails, a hammer is a perfect tool.

    There are a few aspects to macros that make them "bad" (I’ll expand on each later, and suggest alternatives):

    1. You can not debug macros.
    2. Macro expansion can lead to strange side effects.
    3. Macros have no "namespace", so if you have a macro that clashes with a name used elsewhere, you get macro replacements where you didn’t want it, and this usually leads to strange error messages.
    4. Macros may affect things you don’t realize.

    So let’s expand a little here:

    1) Macros can’t be debugged.
    When you have a macro that translates to a number or a string, the source code will have the macro name, and many debuggers can’t "see" what the macro translates to. So you don’t actually know what is going on.

    Replacement: Use enum, const T, or constexpr T

    For "function-like" macros, because the debugger works on a "per source line where you are" level, your macro will act like a single statement, no matter if it’s one statement or a hundred. Makes it hard to figure out what is going on.

    Replacement: Use functions – inline if it needs to be "fast" (but beware that too much inline is not a good thing), and constexpr if it’s needed at compile time.

    2) Macro expansions can have strange side effects.

    The famous one is #define SQUARE(x) ((x) * (x)) and the use x2 = SQUARE(x++). That leads to x2 = (x++) * (x++);, which, even if it was valid code [1], would almost certainly not be what the programmer wanted. If it was a function, it would be fine to do x++, and x would only increment once.

    Another example is "if else" in macros, say we have this:

    #define safe_divide(res, x, y)   if (y != 0) res = x/y;
    

    and then

    if (something) safe_divide(b, a, x);
    else printf("Something is not set...");
    

    It actually becomes completely the wrong thing….

    Replacement: real functions.

    3) Macros have no namespace

    If we have a macro:

    #define begin() x = 0
    

    and we have some code in C++ that uses begin:

    std::vector<int> v;
    
    ... stuff is loaded into v ... 
    
    for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
       std::cout << ' ' << *it;
    

    Now, what error message do you think you get, and where do you look for an error [assuming you have completely forgotten – or didn’t even know about – the begin macro that lives in some header file that someone else wrote? [and even more fun if you included that macro before the <vector> include – you’d be drowning in strange errors that makes absolutely no sense when you look at the code itself.

    Replacement: Well there isn’t so much as a replacement as a "rule" – only use uppercase names for macros, and never use all uppercase names for other things.

    4) Macros have effects you don’t realize

    Take this function:

    #define begin() x = 0
    #define end() x = 17
    ... a few thousand lines of stuff here ... 
    void dostuff()
    {
        int x = 7;
    
        begin();
    
        ... more code using x ... 
    
        printf("x=%d\n", x);
    
        end();
    
    }
    

    Now, without looking at the macro, you would think that begin is a function, which shouldn’t affect x.

    This sort of thing, and I’ve seen much more complex examples, can REALLY mess up your day!

    Replacement: Either don’t use a macro to set x, or pass x in as an argument.

    There are times when using macros is definitely beneficial. One example is to wrap a function with macros to pass on file/line information:

    #define malloc(x) my_debug_malloc(x, __FILE__, __LINE__)
    #define free(x)  my_debug_free(x, __FILE__, __LINE__)
    

    Now we can use my_debug_malloc as the regular malloc in the code, but it has extra arguments, so when it comes to the end and we scan the "which memory elements hasn’t been freed", we can print where the allocation was made so the programmer can track down the leak.

    [1] It is undefined behaviour to update one variable more than once "in a sequence point". A sequence point is not exactly the same as a statement, but for most intents and purposes, that’s what we should consider it as. So doing x++ * x++ will update x twice, which is undefined and will probably lead to different values on different systems, and different outcome value in x as well.

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

Sidebar

Related Questions

i have asked this question before but no answer was there. so asking again.
I have asked several questions on the buddypress forum but I never get any
I have asked this before, but the more I look at other code examples
I have asked aqbout timezones and date/time before but this is a more specific
Note: A have asked this question at the Superuser community first , but since
I'm sure this must have been asked and answered, but I can't find it...
I asked something similar to this before and never got an answer, here's a
I know other questions have been asked on this but none so far have
I have asked this type of a question before but this one IS different.
I have previously asked this question and since I already accepted an answer, I

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.