Damien LOISON
Visual C++ tips

Several tips that I find useful

 


Book:  C++ 11&14 Tips - Understand novelties with working examples

Book, Terse&Good (2016). Accessible on amazon


 

Other tips:


Select a rectangular portion of the screen

ALT + left click + hold + move:

tip_SelectRec


Switch between UK and US keyboards

Alt + Shift


Display Tabulations and spaces

CTRL + shift + 8 or CTRL+ r , CTRL+w:


Indentation

1.      ALT+F8 : will auto indent the selected text.

2.      TAB and SHIF+TAB: will add or remove one indentation

3.      Replace tabulations by spaces when editing : Tool – Options – Text Editor – C/C++

4.      if you have already a file with tabs you can replace all tabs by spaces by selecting the whole file and: Edit – Advanced – Untabify selected lines


Switch between h and cpp files

There are many utilities which allow switching from header and source files. But why do you want to use something already existing if you can redo it? After some years in financial institutions, you will learn to redo badly what was already badly done … This one being so simple, we do not resist to the pleasure to write our macro:

    ' -------------------------------------------------------

    ' A badly written, but working, VBA code to

    '      switch between cpp header and source

    Sub Open_associate_cpp_or_h()

        Dim sName As String

        Dim sNameToOpen As String

        sName = DTE.ActiveWindow.Caption

        If sName.EndsWith(".h") _

           Or sName.EndsWith(".H") _

           Or sName.EndsWith(".c") _

           Or sName.EndsWith(".C") _

           Or sName.EndsWith(".cpp") _

           Then

            ' open the associate file .h or .cpp

            sName = DTE.ActiveDocument.Name()

            If InStr(sName, ".h") Then

                sNameToOpen = Mid(sName, 1, InStr(sName, ".h")) & "cpp"

            ElseIf InStr(sName, ".C") Then

                sNameToOpen = Mid(sName, 1, InStr(sName, ".C")) & "H"

            ElseIf InStr(sName, ".H") Then

                sNameToOpen = Mid(sName, 1, InStr(sName, ".H")) & "C"

            ElseIf InStr(sName, ".c") Then

                sNameToOpen = Mid(sName, 1, InStr(sName, ".c")) & "h"

            ElseIf InStr(sName, ".cpp") Then

                sNameToOpen = Mid(sName, 1, InStr(sName, ".cpp")) & "h"

            End If

            sNameToOpen = DTE.ActiveDocument.Path & sNameToOpen

            DTE.ItemOperations.OpenFile(sNameToOpen)

        End If

    End Sub

    ' -------------------------------------------------------

Now that we have proved that we can write a badly VBA program, we can search the web for more robust programs. I would recommend Pierre Arnaud's Solution which works if the header files and source files are not stored in the same directory.

Then you have to associate your new macro to a shortcut using tool->Customize->Keyboard… or associate it to a button:

1) R-click on toolbar, select Customize
2) In the Commands tab, under Categories, select Macros then the macro you just made
3) Drag the name of the macro onto the toolbar you want to use (you can make a new one, or add it somewhere)
4) R-click on the button you just added (the Customize Window needs to be open) and change the name to something shorter

The lazy and wise programmer will use the free PhatStudio. In this case ALT+S will work.


Debug

1.    Shortcuts

·         Get tired of F9 (breakpoint), F5 (run) and F9 (remove breakpoint)?
=> CTR+F10 should save you: run to the cursor.

·         CTRL + SHIFT + F10: set next statement in debug mode. No need to use the mouse!

·         Do not forget the usual: F11: step Into, SHIFT+F11: step out, and F10: step over

2.    Do not step into some functions

Useful in a call of a function when parameters are itself function or classes (std::string, boost::shared_ptr, …).
We just have to enter the elements we do not want to step into in the registry. Depending of the version of Visual

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.0\NativeDE\StepOver

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.1\NativeDE\StepOver

HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\8.0\NativeDE\StepOver

HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\9.0\NativeDE\StepOver

HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\10.0\NativeDE\StepOver

To avoid to step into std::string we include the entry

"12"="std\\:\\:string\\:\\:.*=NoStepInto"

We have included several namespaces in the file Visual_NativeDE_StepOver.reg: std, ATL, boost. If you want to go into a function of boost, std, or ATL, you include a StepInto for the corresponding function. For example if you want to step into std::vector, include the following entry:

"30"="std\\:\\:vector\\<.*\\>\\:\\:.*=StepInto"

The first number is the priority (30)  and must be bigger than the one corresponding to std (18 in the reg file).

Download : Visual_NativeDE_StepOver.reg

More info http://blogs.msdn.com/b/andypennell/archive/2004/02/06/69004.aspx

3.    Expand your own class in the debugger

Visual 2008 and 2010 are much better to debug than 2003 or 2005, without speaking of Visual 6. However if this does not work you can have a try at the file autoexp.dat:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger
C:\Program Files\Microsoft Visual Studio 10.0\Common7\Packages\Debugger
You should be able to copy some examples from the file. For example all debugging for STL container are there. A tutorial can be found there: http://www.idigitalhouse.com/Blog/?p=83 and in any other case try google.
The only Tip in this page that I have not used. It is here for reference.


Shortcuts in Editing/Moving between windows

These are the shortcuts that I use regularly:

·         CTRL+(SHIFT)+- : Moves to the previously (next) browsed line of code.

·         CTRL+]: Moves the cursor to the matching brace

·         CTRL+TAB : Navigate between the open windows

·         TAB and SHIFT TAB: add or remove indentation

·         ALT+F8 : will auto indent the selected text

·         CTRL+SPACE: Completes the current word

·         CTRL+SHIFT+INS: Cycles through the clipboard ring (CTRL+SHIFT+V for version < 2005)

·         CTRL+SHIFT+SPACE: Displays the name, return value, and parameters of the function call

·         CTR+u: lower case, CTRL+SHIFT+U : upper case (I knew a colleague who used word …)

·         CTRL+SHIFT+F: Find or Replace in all files in the solution

·         CTRL+F3 +(SHIFT): Look for next (previous) select text

·         CTRL+(SHIFT)+F12 : Go to definition (declaration).

·         CTRL+F4: Closes the current document window

·         F6: Moves to the next pane of a split pane view of a single document

 

All Visual 2005 shortcuts. Also Shortcuts

 


Expand Macros

You have a beautiful macro which does not work or you want to understand it and debug it. Choose to expand the macro in your file:

When compiling It will create a text file “*.i” that you can investigate.


Intellisense does not work

Delete the “*.ncb” file in your solution folder. Could work but not always. For a big project with quite a lot of macros, my intellisense stopped to work. Annoying, but you learn to live without it!

Also look at for problems with precompiled headers: http://blogs.msdn.com/b/vcblog/archive/2011/03/29/10146895.aspx


Perforce plug-in

If you are happy with the P4 add-in for Visual Studio this tip is not for you. Personally I removed it because it was too slow. I made a macro to check out the current file from Visual studio. This old ugly macro will create the command file to check out the file and run it using p4 command line. The point to write to a file is to be able to debug it when there is a problem. That’s all. You have to check-in manually. Not intelligent, and not beautiful, but it is fast and it works well enough for me.

1. To be able to use it you need to have installed the p4 with the command line. If you have not done it, you have to uninstall the p4 and reinstall it checking the option "command line"

2. Copy the following macro in the "MyMacro" (open it with Alt+F11)

3. You have to specify the client inside the macro (“vClient = …”).

4. Use a shortcut to this macro using Tools->Customize->Keyboard   and then assign it to a shortcut.


Add-in for Visual

There are several add-ins accessible. Personally I used mainly:

1.      STLFilt simplifies and/or reformats long-winded C++ error and warning messages, with a focus on STL-related diagnostics. Really useful. A little bit annoying to install (read the README.txt!), and do not forget to add the option /WL in the compilation command line of Visual.

2.      Visual Local History : local file history à la Eclipse. For 2005, and 2008. Create backup when saving a file. Indispensable if you do not have a source control.

3.      Fast file navigation with a filter

a.       PhatStudio. (ALT+O): Include switch between source and header files (ALT+S).

b.      VS File Finder: Very good but I have some problems to find the add-in for some versions of Visual.  For Visual 2003. For Visual 2008,

c.       SonicFileFinder: Alternative.

4.      Profiler "Very Sleepy" : open-source profiling software. GUI outside Visual C++.

5.      Visual Leak Detector : open-source memory leak detection system for Visual C++. Follow these instructions.

6.      Incredibuild. Distributed builds. Not free but so useful.

7.      Tomato: I used it at Barclays&Citi and some of my colleagues were great fans. However I do not find it more useful than PhatStudio. The only good point is that F12 is almost broken in Visual 2010 and so Ctr+G can be used instead. .

8.      cppcheck static analysis tool for C/C++ code. Can be useful. However it is not integrated to Visual, so you will need to create your command line.

9.      CancelFailedBuild Cancels a failed build as soon as the first failure occurs. Very useful

10.  Viafora: coloring brackets Enhances the VS text editing experience with Rainbow Braces, XML highlighting and more. Very very useful.

11.  Remove/hide the 'External Dependencies' folder in Solution Explorer Options dialog (from the Tools menu). On the right side panel expand the Text Editor section, then expand C/C++ and then click on Advanced. Set the Disable External Dependencies Folder to True and restart Visual Studio.

 



Go back to Damien Loison