Formatting C ++ Code

I am using VS2005 and VS2008 now, with C #. Without any additional tools, I could hit ctrl k + d and the code would reformat nicely. Why doesn't C ++ do this? This is Visual Studio after all. Is there a way to enable it?

The second part of this question is related, for those who have used resharper with C #, is there a similar tool for C ++?

+2


source to share


6 answers


  • You can use Ctrl-K + Ctrl-F to select the format.

  • Link



+6


source


It probably is, but maps to a different keyboard shortcut. For example, on my VS2008 set Ctrl + K , D does not map to anything but Ctrl + E , D maps to Edit | Advanced | Format Document.



+3


source


You can always use AStyle to format your code. Works great!

+2


source


For me, text has always been selected, CTRL + K, F. This is not the same as in Visual C #; there are no customization options next to most of the main ones.

+1


source


Edit: In 2017, this answer seems to be outdated. I recommend clang-format for "nothing", for "anyone".

Original answer: Ontopic: In VS2008 Express Format Selection select ALT + F8 and CTRL + K, CTRL + D is mapped to document format. This is probably the same in the professional versions of VS2008.

Less Ontopic: I try to avoid formatting source code for C ++ as I feel like they usually do more harm than good. What for? Codeformaters must "read" the code and understand what the various instructions in the code do. It's pretty easy, for example Java where the syntax is simple. C ++ is a very complex language. There are 10 ways to do anything. You can use macros, defines, typedefs and whatnot. Classes are usually defined / declared in two different files. This is also the reason why Intellisense seems to fail quite often - VS2008 fails to parse the code correctly. It is very difficult for an IDE to know what all statements are and how to format them. While it can work fine in most cases, you can be sure that it will give poor results from time to time.My guess is that Microsoft didn't spend too much time setting up the code formatting for C ++ as they understand that most C ++ programmers do it by hand. I rarely skip it, except when my colleague doesn't meet the company's standard.

+1


source


** You can use bcpp [apt-get install bcpp] to format C / C ++ code. This program will work under Linux / Unix and MS DOS V3.3

There is a config file, so you can get creative.

** An interesting tool for C / C ++ / C # / Java suggested by Qt is astyle [apt-get install astyle]

0


source







All Articles