2

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

A second part of this question is related, for those who've been using resharper with C#, is there a similar tool for C++?

luke
  • 32,714
  • 7
  • 56
  • 80

6 Answers6

6
  1. You can use Ctrl-K+Ctrl-F for "format selection".

  2. Link

Community
  • 1
  • 1
bmeric
  • 1,090
  • 1
  • 17
  • 27
3

It's probably there, but mapped to a different keyboard shortcut. For example, on my VS2008 install Ctrl+K,D doesn't map to anything but Ctrl+E,D maps to the Edit|Advanced|Format Document command.

Scott Dorman
  • 40,345
  • 11
  • 74
  • 107
2

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

1

For me it's always been select text, CTRL + K, F. It doesn't do as well as in Visual C# though; there are no customization options beside the most basic ones.

Asik
  • 19,392
  • 5
  • 60
  • 119
1

Edit: in 2017 this answer seems outdated. I recommend clang-format for "anything", for "anyone".

Original answer: Ontopic: In VS2008 Express Format Selection is ALT+F8 and CTRL+K, CTRL+D is mapped to Format Document. It's probably the same in professional versions of VS2008.

Less ontopic: I tend to avoid source code formatters for C++ as I feel they generally often does more harm than good. Why? Codeformaters has to "read" the code and understand what the different code statements do. This is quite easy in e.g. Java where the syntax is straight forward. C++ is a very complex language. There's 10 ways to do just about anything. You can use macros, defines, typedefs and whatnot. Classes are usually defined/declared in two different files. This is also the reason that Intellisense seems to fail quite often - VS2008 fails parsing the code correctly. It's very hard for an IDE to know what all statements is and how to format them. Although it may work ok most of the time, you can be pretty sure that it will give bad results now and then. I'm guessing Microsoft didn't spend too much time customizing the code formatter for C++ as they realize that most C++-programmers do this by hand anyways. I rarely miss it, except when some co-worker of mine isn't following the company standard.

larsmoa
  • 11,450
  • 5
  • 54
  • 82
0

**For C/C++ code formatting you can use bcpp [ apt-get install bcpp ]. This program will run under Linux/Unix and MS DOS V3.3

There is config file so you can be creative.

**Interesting tool for C/C++/C#/Java proposed by Qt is astyle [ apt-get install astyle ]

Amit Vujic
  • 1,199
  • 1
  • 19
  • 27