Search code examples
c++c-preprocessorpreprocessor-directive

Preprocessor Directive "Using"


From my previous knowledge in learning C, I know that preprocessor directive like #include , #define is ain't a statement that's why as the name implies , it is process before the program is compiled , therefore there's no need for us to append a ; at the end of it.

In C++, it introduces me a new directive that is using , but why this directive append a semicolon? I thought it's just like the previous directive I learn where it's not a statement?


Solution

  • using is not a preprocessor directive. It is seen and analyzed by the compiler proper.

    The fact that you often don't put a ; at the end of #define macros is because they are processed as "simple" text replacement by the preprocessor, e.g:

    #define SOMETHING "abcd";
    
    ...
       if (strcmp(thing, SOMETHING) == 0) { ... }
    ...
    

    would be a compiler error since the compiler would see:

       if (strcmp(thing, "abcd";) == 0) { ... }
                           //  ^ invalid here