2

I am have a macro TYPELIST which takes variadic arguments. I want to have something like

typedef TYPELIST(A
                ,B
                ,C
                ,D
#ifdef BLA_
                ,E
#endif
                ,F)

This works perfectly with gcc. However, when I try to compile it with MSVC it parses ifdef and endif as macro arguments. I know one way would be to put the macro call inside an ifdef. However, if I have a huge list and if I want to include different classes depending on different macros defined, it would become tedious. Is there a particular reason why this works in gcc and not with MSVC?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Shubham
  • 352
  • 3
  • 14

2 Answers2

1

Using #ifdef inside a macro isn't legal. I am sort of surprised that gcc allows this. I'm afraid you have to put the #ifdef around the entire definition, i.e.

#ifdef BLA_
    typedef TYPELIST(a,b,c,d,e,f)
#else
    typedef TYPELIST(a,b,c,d,f)
#endif
digory doo
  • 1,978
  • 2
  • 23
  • 37
0

According to the standard (§16.3.4/3), "The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one,[...]". If g++ processes the #ifdef/#endif here, it's an error in the compiler (at least if you've requested standards conformance, e.g. with -std=...).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • But it works with g++ and clang++. I also tried to force standard conformance using `-std=c++03` but it still doesn't complain. – Shubham Oct 01 '13 at 08:44
  • Actually, it's not an error, it's just UB. 16.3/11 (talking about arguments to a function-like macro): "If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives, the behavior is undefined." – Angew is no longer proud of SO Oct 01 '13 at 08:49
  • Ah yes. There is a difference between "expanding to a preprocessor directive" and preprocessing tokens in an argument list. So it's not an error as far as the standard is concerned; just a poor choice of how they handle undefined behavior (although instead of a choice, it may be related to how they tokenize). – James Kanze Oct 01 '13 at 09:00