Visual C++ Code Snippets

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here

In Visual Studio, you can use code snippets to add commonly-used code to your C++ code files. In general, you can use code snippets in much the same way as in C#, but the set of default code snippets is different.

You can either add a code snippet at a particular location in your code (insertion) or surround some selected code with a code snippet.

Inserting a Code Snippet

To insert a code snippet, open a C++ code file (.cpp or .h), click somewhere inside the file, and do one of the following:

  • Right-click to get the context menu and select Insert Snippet

  • In the Edit / IntelliSense menu, select Insert Snippet

  • Use the hotkeys: CTRL + K + X

    You should see a list of choices beginning with #if. When you select #if, you should see the following code added to the file:

#if 0  
  
#endif // 0  

You can then replace the 0 with the correct condition.

Using a Code Snippet to Surround Selected Code

To use a code snippet to surround selected code, select a line (or multiple lines) and do one of the following:

  1. Right-click to get the context menu and select Surround With

  2. In the Edit / IntelliSense menu, select Surround With

  3. Use the hotkeys: CTRL + K + S

    Select #if. You should see something like this:

#if 0  
#include "pch.h"  // or whatever line you had selected  
#endif // 0  

You can then replace the 0 with the correct condition.

Where can I find a complete list of the C++ code snippets?

You can find the complete list of C++ code snippets by going to the Code Snippets Manager (on the Tools menu) and setting the Language to Visual C++. In the window below, expand Visual C++. You should see the names of all the C++ code snippets in alphabetical order.

The names of most code snippets are self-explanatory, but some names might be confusing.

Class vs. classi

The class snippet provides the definition of a class named MyClass, with the appropriate default constructor and destructor, where the definitions of the constructor and destructor are located outside the class:

class MyClass  
{  
public:  
MyClass();  
~MyClass();  
  
private:  
  
};  
  
MyClass::MyClass()  
{  
}  
  
MyClass::~MyClass()  
{  
}  

The classi code snippet also provides the definition of a class named MyClass, but the default constructor and destructor are defined inside the class definition:

class MyClass  
{  
public:  
MyClass()  
{  
}  
  
~MyClass()  
{  
}  
  
private:  
  
};  

For vs. foreach vs. forr vs rfor

There are four different for snippets that provide different kinds of for loops.

The for snippet provides a for loop in which the condition is based on the length (in size_t) of an object:

for (size_t i = 0; i < length; i++)  
{  
  
}  

The foreach snippet provides a for each loop that iterates over the members of a collection:

for each (object var in collection_to_loop)  
{  
  
}  

The forr snippet provides a reverse for loop in which the condition is based on the length (in integers) of an object:

for (int i = length - 1; i >= 0; i--)  
{  
  
}  

The rfor snippet provides a range-based for loop (link):

for (auto& i : v)  
{  
  
}  

The destructor snippet (~)

The destructor snippet (~) shows different behavior in different contexts. If you insert this snippet inside a class, it provides a destructor for that class. For example, given the following code:

class SomeClass {  
  
};  

If you insert the destructor snippet, it provides a destructor for SomeClass:

class SomeClass {  
    ~SomeClass()  
    {  
  
    }  
};  

If you try to insert the destructor snippet outside a class, it provides a destructor with a placeholder name:

~TypeNamePlaceholder()  
{