[C++11 16.1]
, [C++11 16.5]
and, incidentally, [C99 6.10.1/4]
all say that this is invalid.
if-group:
# if
constant-expression new-line groupopt
# ifdef
identifier new-line groupopt
# ifndef
identifier new-line groupopt
Only one identifier is legal.
GCC's own documentation agrees.
My own tests suggest that only the first identifer is accepted, and the second is simply discarded; this may be to ease the implementation, but the standard does require a diagnostic here, so you should see this when you use the -pedantic
flag at least†.
#include <iostream>
using namespace std;
#define A
#define B
int main() {
#ifdef A
std::cout << "a ";
#endif
#ifdef B
std::cout << "b ";
#endif
#ifdef C
std::cout << "c ";
#endif
#ifdef B C
std::cout << "bc ";
#endif
#ifdef C B
std::cout << "cb ";
#endif
return 0;
}
// Output: "a b bc"
// Note: "cb" *not* output
† Coliru's installation of GCC emits it with or without -pedantic
.
g++ myprogram.cpp -o myprogram | grep ! warning
– Cordillerasgrep
syntax is wrong, and gcc writes diagnostics tostderr
). In any case, I think you're speculating; I'd like to know how the OP didn't get a warning. OP: Did you intentionally disable warnings? – Coadjutor#ifdef
. – Coadjutor