3

Can i redefine keywords with #define in C?

I found this in C++ standards:

ISO/IEC 14882:1998 and ISO/IEC 14882:2003

17.4.3.1.1 Macro names [lib.macro.names]

2 A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.

164) It is not permissible to remove a library macro definition by using the #undef directive.

ISO/IEC 14882:2011

17.6.4.3.1 Macro names [macro.names]

2 A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 3, or to the attribute-tokens described in 7.6.

So, we can't redefine keywords in C++98 / C++03 if we include any header files from Standard C++ Library, while in C++11 we can't do it in any translation unit, right?

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

3 Answers3

2

Can you? Yes, most compilers don't forcibly prevent it.

Should you? No, (almost) never, it's (usually) a horrible idea. All sorts of things may break, and as with most preprocessor nonsense, it's going to be very difficult to debug.

Therefore, can you legally? No.

In the same fashion, you can have macros or variables that start with __ or add things to the std:: namespace or all sorts of other Very Bad Things. However, most rules like this have Very Good Reasons (conflicts related to keywords or name resolution, mostly). While it may be physically possible, there are few (if any) times you need to and fewer still when you should.

ssube
  • 47,010
  • 7
  • 103
  • 140
2

In C standard, it is not allowed.

C11, § 6.4.1 Keywords

[...]

The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise.

md5
  • 23,373
  • 3
  • 44
  • 93
  • 1
    It's not allowed in the C or C++ spec, that doesn't actually stop you from doing it (there are a lot of things that are against-spec but allowed by compilers or mistakenly done by programmers). This just restates the question. – ssube Sep 05 '12 at 17:31
  • The question is "Can i redefine keywords with #define in C?". Stricly speaking, the answer is no. It is also often a bad pratice, even if some compilers accept it... – md5 Sep 05 '12 at 17:32
  • 3
    The quote says "in translation phases 7 and 8". Preprocessing is phase 4. – sepp2k Sep 05 '12 at 17:34
  • 1
    Uhm... Good catch. So, is it allowed? – md5 Sep 05 '12 at 17:35
1

There are several aspects in this issue:

  • If you will redefine keywords, in 95% of compilers this will work fine regardless of what is written in the standards primarily because this was working in old compilers;
  • You can never be sure that this will work in the next version of your compiler;
  • You should avoid this from the practical stand point. The code will be more difficult to read and understand.

The bottom line: Do you really need this? What will you get?

p.s. I saw several times that try-catch were redefined in various headers.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51