Вопрос

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?

Это было полезно?

Решение

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

Другие советы

using can be thought as being a declaration, like e.g. typedef is.

And you can ask the compiler to output the result of preprocessing, e.g. with g++ -C -E but there is no simple way to ask it to output the effects of using

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top