Nested comments in C ++

This should be a common problem, and maybe seem like some kind of question here, but I believe that the adversary is the best way to comment on a few lines (rather methods) in C ++ that contain comments in them. I looked through some posts about SO, but could not get complete information about using something like # 0.

I read this post Nested Comments in Visual C ++? but I'm not on a windows platform.

+6
source share
4 answers

You are almost right; essentially, an "if-def" section of code is suggested. What you want to do is use the precompiler #if directive to block the code for you. The example below shows that I want to ignore everything between if and endif.

 #if 0 /* Giant comment it doesn't matter what I put here */ // it will be ignored forever. #endif 

To answer your question as a whole, though; there is no way to have complex comments, i.e.

 /* /* */ <--- this closes the first /* */ <--- this dangles. 
+11
source

Use whatever tools your editor provides to add // a the beginning of all lines.

For example, in Vim, you can mark lines as a visual block, and then insert at the beginning of all lines with I// . In Visual Studio, you can use the CTRL-KC shortcut to comment code blocks.

+2
source

Material between #if 0 and #endif will be ignored by the compiler. (Your preprocessor may actually disable it before the โ€œcompilerโ€ can even look at it!)

 #if 0 /* 42 is the answer. */ Have you tried jQuery? @Compiler Stop ignoring me!! #endif 

You will have better control if you use #ifdef s:

 // #define DEBUG #ifdef DEBUG MyFunction(); std::cout << "DEBUG is defined!"; #endif // Later in your code... #ifdef DEBUG std::cout << "DEBUG is still defined!"; #endif 

Just uncomment the first line and your #ifdef DEBUG code #ifdef DEBUG suddenly become visible to the compiler.


PS This should eliminate the confusion:

 /* cout << "a"; /* cout << "b"; */ cout << "c"; */ 

The output should be "c" if your compiler does not give you any errors for the last */ .

+1
source

Another route, assuming you are using Visual Studio, is a convenient key combination for commenting all the selected code, adding // in front of each line. CTRL+K + CTRL+C to comment and CTRL+K + CTRL+U to uncomment.

+1
source

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


All Articles