Visual Studio 11: C++ IntelliSense Code Snippets

Introduction

One of the new features of Visual Studio 11 is inserting C++ code snippets, i.e. easily adding already made C++ code at a specified insertion point or around a selected block. This is useful and may save programming time when it’s necessary to add commonly-used code, for example preprocessor directives (if, else, for, while, switch, try/catch blocks, and so on). Visual Studio 11 comes with a set of C++ code snippets but we can create and use our own, as well.

An Example of Code Often Manually Added

Let’s say we have created a Win32 DLL project, which has to export some classes and functions. One common way to include the same header file, containing exported class definitions or exported function declarations, in the DLL and in client modules is using preprocessor definitions as in the following example:

#ifdef MYLIB_EXPORTS
#define MYLIB_EXP __declspec(dllexport)
#else
#define MYLIB_EXP __declspec(dllimport)
#endif

class MYLIB_EXP CFoo {/*...*/};
MYLIB_EXP void foo();

 

If we have to implement many DLL projects, each containing many exported symbols, there is a lot of code that has to be manually added. Of course, copy-pasting then modifying by hand is a solution but the Visual C++ code snippets feature offers a better and faster solution.

Using Code Snippets

Before going further, we have to notice there are two methods to add code snippets:

  • at an insertion point (expansion snippets)
  • around a selected block of code (surround-with snippets)

Next, we demonstrate step-by-step how to use each of the two methods to automate the inserting of the above sample of code.

 

Using an Expansion Snippet

    1. Click in the source editor at the point where you want to insert the code snippet.

      Set Insertion Point
      Figure 1: Set Insertion Point

       

    2. Keeping Ctrl key pressed, push ‘K’ then ‘X’. Alternatively, we can select the “Edit/IntelliSense/Insert Snippet…” menu item.
      A list of available code snippets appears. Select the desired one (“#ifdef”) then hit the Enter key.

      Insert Snippet
      Figure 2: Insert Snippet

    3. The inserted code snippet contains a highlighted default name (“DEBUG”), which can be replaced with our desired one.

      Default Code Snippet
      Figure 3: Default Code Snippet

    4. Replace “DEBUG” with “MYLIB_EXPORTS” and hit the Enter key.

      Complete Code
      Figure 4: Complete Code

       

    5. Complete by hand the code betwen “#ifdef” and “#endif”

 

Note: Code snippets may have “shortcuts”. In the case of our example, the shortcut is “#ifdef”. That means, to insert this snippet we can simply type “#ifdef” in the editor, and then hit the Tab key.

Using a Surround-with Snippet

  1. In the source editor, select code to surround.

    Select Code to Surround
    Figure 5: Select Code to Surround

  2. Keeping the Ctrl key pressed, push ‘K’ then ‘S’. Alternatively, we can select the “Edit/IntelliSense/Surround With…” menu item.
    A list of available code snippets appears. Select the desired one (“#ifdef”) then hit the Enter key.

    Surround With
    Figure 6: Surround With

  3. The inserted code snippet contains a highlighted default name (“DEBUG”), which can be replaced with our desired one.

    Default Surround Code
    Figure 7: Default Surround Code

  4. Replace “DEBUG” with “MYLIB_EXPORTS” and hit the Enter key.

    Complete Code
    Figure 8: Complete Code

Quite nice, until now! However, we still have some manually added code. Next page, we create our own code snippet to make all in one shot.

More by Author

Must Read