Why write a literal followed by a semicolon in c / C ++?

I am looking at some kind of code and I found the following macro:

int foo = 0; (foo); 

It compiles just fine. In fact, it seems that

 0; 

- A valid line of code in C / C ++.

I took a look at assembly assembly in debug and release assemblies (on msvc), and that doesn't make any difference to the assembly. My test was simple though (with and without (foo); ):

 int main() { int foo = 0; (foo); return 0; } 

My question is, why does anyone want to do this? I am sure (foo); is in the macro for some reason, but I'm not sure why.

For context, the macro found is as follows (I renamed the variables):

 #define MY_MACRO int _foo = 0; (_foo); UINT _bar = CP_THREAD_ACP; (_bar); LPCWSTR _baz = NULL; (_baz); LPCSTR _thing = NULL; (_thing) 

In code, this is simply called

 MY_MACRO; //other code... 
+5
source share
2 answers

As suggested by Fred Larson in the comments, it can be used to suppress compiler warnings. I tried this code:

 int foo = 0; foo; 

I debugged the code and the compiler completely skipped the second line, so it seems to be doing nothing. Of course, if you are afraid that these lines will affect the macro, you can always go through the code using the debugger, as I did.

0
source

This may be the result of expanding the macro.

Maybe someone will be stupid.

This may be suppressing compiler warnings.

In any case, such statements are completely legal in the language and do not have a real impact on the program. Any competent compiler will simply delete them at compile time.

0
source

Source: https://habr.com/ru/post/1270694/


All Articles