Why Does VS 2010 'Comment' Keyboard Shortcut Change in C++?

只愿长相守 提交于 2020-01-15 07:54:07

问题


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 /* */


来源:https://stackoverflow.com/questions/5708571/why-does-vs-2010-comment-keyboard-shortcut-change-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!