I've found a interesting code snippet that works pretty strange
#include <iostream>
#include <cstdio>
#define DEBUG_CMD(cmd) \
do { \
cmd; \
} while (false)
using namespace std;
int main()
{
cout<<"Hello World";
DEBUG_CMD({
int x = 1;
int y = 1;
//int x= 1, y =1; // enable this and comment two lines above will yield an error
printf("x: %d y: %d\n", x, y);
});
return 0;
}
when I define x, y seperately, the code can run. but when I define x y in the same statement sperated by comma, the compiler complains about the syntax error.
main.cpp:26:6: error: macro "DEBUG_CMD" passed 2 arguments, but takes just 1
});
^
main.cpp: In function ‘int main()’:
main.cpp:21:5: error: ‘DEBUG_CMD’ was not declared in this scope
DEBUG_CMD({
^~~~~~~~~
I wonder what's happening here.
TIPS: you can compile it at https://onlinegdb.com/ryUuyZEmO and it also gives you the same error.
do { \
__VA_ARGS__; \
} while (false)` https://ideone.com/d0Xp9V – 273K Mar 08 '21 at 20:08