2

I commented on this answer some time ago regarding how visual studio comments out code with // or /* */. I was thinking to revise the answer (to include my findings) but I had to test it first, which kind of confused me.

My finding is that depending on what you comment when you press Ctrl - K, Ctrl - C you will get either // or /* */.

First example:

<start selection here>    code();
                          someCall();
                          thirdCall();<end selection here>

this will produce the following:

//code();
//someCall();
//thirdCall();

Second example:

    <start selection here>code();
                          someCall();
                          thirdCall();<end selection here>

this will produce the following:

/*code();
someCall();
thirdCall();*/

Third example

    <start selection here>code();
                          //someCall();
                          thirdCall();<end selection here>

this will produce the following:

//code();
////someCall();
//thirdCall();

Note that example 2 and 3 is the exact same selection, but the comment makes Visual Studio interpret it differently.

Why is this?

Community
  • 1
  • 1
default
  • 11,485
  • 9
  • 66
  • 102

1 Answers1

2

The approach one would expect is to use // for any selection that is made up entirely of complete lines, and /*...*/ for anything that starts/ends mid-way along a line.

...which is what it seems to actually do.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • What do you mean by complete lines? – default May 23 '10 at 11:12
  • If you put the cursor at the left edge (on column 0) and then press shift+down, you will select a complete line. That is, all the text from column 0 to the end of the line, plus the newline at the end. – Jason Williams May 23 '10 at 11:33
  • exactly, that is [1]. But when selecting from column 4 (where the text starts) you get /* */. I hope my edit made it clearer.. I also found out that it has something to do with comments..? – default May 23 '10 at 11:43
  • It seems this is a C++ specific behaviour. I can understand it using // instead of /* */ if the lines to comment include a multiline comment (because they cannot be nested). I presume that if you include a // comment it is taken as a "hint" that you simply prefer // comments to /* ones? – Jason Williams May 23 '10 at 20:14