I’m trying to use the following code in C++. Can someone tell me why it is showing error?
#define def namespace;
using def std;
int main(){
return 0;
}
while the following code is working fine
#define def namespace std;
using def;
int main(){
return 0;
}
It is because of the embedded semicolon:
Note that
#defineitself doesn’t need a semicolon to terminate the definition, so if you use one it becomes part of the text that will be inserted wherever the macro is used.After preprocessing, the first example will turn the
usingline into:which has syntactic problems, obviously.
The fix is to remove the trailing semicolon in the
#defineline, like so:You should figure out how to read the pre-processed code with your compiler, it’s always instructive when straightening out macro-induced confusion.