问题
For me, Visual Studio's Ctrl + K, Ctrl + C keyboard shortcut is used to comment-out the selected lines. When editing C++, this sometimes uses block comments (/* */
) and sometimes uses line comments (//
). Why does it change? How does it decide which to use when?
回答1:
A couple other discussions on the topic:
Visual studio feature - commenting code Ctrl K - Ctrl C
visual studio C++ toggle comment ? comment while not whole line is selected?
Based on my own tinkerings, and what was said in those articles...
It's based on the start/end of the selection. It seems to use double slashes //
whenever you start your selection at the beginning of the line AND end it at the end of a line.
It will use /* */
notation whenever the selection occurs midway through lines.
IE:
If I have the code
int main () {
return 0;
}
and highlight only int main
, it will convert it to /*int main*/
.
If I highlight the entire code section, starting after the indent tab, it will convert it to
/*int main () {
return 0;
}*/
But if I highlight the section starting before the indent tab, it converts it to
//int main () {
// return 0;
//}
回答2:
Summary of links under Zhais' answer. Because following links is hard!
- Selecting entire lines (including leading whitespace) will use
//
- Selecting at least one partial line
- If a
//
comment is included, will use//
- Otherwise, will use
/* */
- If a
来源:https://stackoverflow.com/questions/5708571/why-does-vs-2010-comment-keyboard-shortcut-change-in-c