2
#define MY_MACRO 3 --> in A.h
#define MY_MACRO 45 --> B.h

//In C.cpp

#include "A.h"
#include "B.h"

..
..
..
int my_value = MY_MACRO;

What will my_value be 3 or 45?

developer
  • 4,744
  • 7
  • 40
  • 55
Abdus Khazi
  • 453
  • 3
  • 17
  • 6
    what did you try? what happened? – Sourav Ghosh Oct 24 '16 at 10:13
  • In a huge project, there are 2 definishions of the same macro. We get compiler warnings which I want to resolve. Do not know which one I should remove. – Abdus Khazi Oct 24 '16 at 10:15
  • 4
    [wow, i needed over 20 seconds to verify.](http://coliru.stacked-crooked.com/a/d4390ca25cefd4d0) – asu Oct 24 '16 at 10:15
  • the latest defined one is the one used. – asu Oct 24 '16 at 10:15
  • 2
    @AbdusSalamKhazi If you have a huge project, you should not have 2 macros with different values. Which one to remove depends on the situation. You should look at it in detail and remove the incorrect one. – Rishikesh Raje Oct 24 '16 at 10:18
  • @RishikeshRaje, yes dude... that is exactly the problem. I am not able to figure out which one I should remove and what its implications are. – Abdus Khazi Oct 24 '16 at 10:19
  • 1
    Only you can know that, through studying and understanding the code, and determining how it is supposed to behave. If you cannot do that for whatever reason (why?), ask a colleague on your team for help. – Lightness Races in Orbit Oct 24 '16 at 10:20
  • 3
    @Asu - great suggestion! Except you are wrong. C++ is not a good language for saying "just try it", because it has [*Undefined Behaviour*](https://en.wikipedia.org/wiki/Undefined_behavior). ([See paragraph 2](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf#444)). However, the [behaviour with gcc is to use the latter definition](https://gcc.gnu.org/onlinedocs/cpp/Undefining-and-Redefining-Macros.html), this is not guaranteed everywhere! – BoBTFish Oct 24 '16 at 10:20
  • @BoBTFish: It's a dang good start though – Lightness Races in Orbit Oct 24 '16 at 10:21
  • 1
    Depending what you know of the code, it won't be safe to remove either: any use _between_ the two definitions will use the first one; any use _after_ the second will use the second. You _could_ try `#undef MY_MACRO` just before the second, but the danger is that some of the uses beyond that point are _intending_ to use the first one. The only safe option is to untangle the two duplicates into separate names. – TripeHound Oct 24 '16 at 10:22
  • @AbdusSalamKhazi It really depends on your project, and any of us cannot comment as to which macro to remove. – Rishikesh Raje Oct 24 '16 at 10:22
  • @TripeHound, namespacing a macro in c++? I am not sure if that is possible http://stackoverflow.com/questions/11791187/is-it-possible-to-place-a-macro-in-a-namespace-in-c – Abdus Khazi Oct 24 '16 at 10:26
  • 1. Don't do these macros in header files, it's bad practice for exactly this reason. 2. Consider using #undef if required. – UKMonkey Oct 24 '16 at 10:44

2 Answers2

6

From the standard (draft) [cpp.replace] §2:

An identifier currently defined as an object-like macro (see below) may be redefined by another #define preprocessing directive provided that the second definition is an object-like macro definition and the two replacement lists are identical, otherwise the program is ill-formed. [...]


What happens when you redefine a macro?

When the new definition is different, your program is ill-formed. The compiler is required to show you a diagnostic message (a warning, or an error). The behaviour of an ill-formed program is not defined by the standard. The compiler is free to refuse compiling the program.

What will my_value be 3 or 45?

Whatever your pre-processor/compiler chooses. Or the compiler could refuse to compile it.


Technically, the program would become well-formed if you first undefined the macro. Then the defined value would obviously be the newly defined. However, I do not recommend this, because you can then easily break other rules depending on the order of inclusion of headers in multiple translation units.

Most likely, the two macros are supposed to be separate entities, and there are different files that expect the definition from one header, and not the other. The correct solution is to give each a unique name by renaming one, and change the dependant files to use the new name. Figuring out which files use which definition is may be a challenge. While you're at it, you may want to replace the macro with a constexpr variable.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

I thought

What happens when you redefine a macro?

It gives redefinition warning for macro.

Still

if you avoid warning and try to run your program

then all we know that in cpp all statements are executed sequentially from the top to the bottom, so whatever the last definition of macro is printed.

What will my_value be 3 or 45?

in your code 45 will be taken as macro definition.

chex
  • 249
  • 1
  • 12