A quick way to insert enable guard in Visual Studio

I want to automatically insert include guards into newly created header files in Visual Studio 2012. Is there any predefined snippet for this purpose?

EDIT: I know that once #pragma and its widespread compiler support. But our coding style makes me use the included guards.

+6
source share
5 answers

In visual studio 2012 use a keyboard shortcut

Ctrl+K,Ctrl+S 

It allows you to surround the selected code with code fragments, such as:

#if , #ifdef , #ifndef , if , class , do , enum and many others

.. or specify your own: http://msdn.microsoft.com/en-us/library/ms165394.aspx

+5
source

#pragma once ?

But no, I don’t know anything that #ifndef , etc., automatically inserts. for you.

+1
source

Since you are marking C ++, you must add classes with the built-in wizard. The wizard creates the #pragma once directives. It is even available for other compilers: # pragma once so that you do not break plattform cross-compatibility.

However, you can create a VS macro like this:

 Option Strict Off Option Explicit Off Imports System Public Module HeaderGuard Sub InsertHeaderGuard() Dim filename As String = DTE.ActiveDocument.Name filename = filename.ToUpper().Replace("."c, "_"c) DTE.ActiveDocument.Selection.Text = "#ifndef " + filename DTE.ActiveDocument.Selection.NewLine() DTE.ActiveDocument.Selection.Text = "#define " + filename DTE.ActiveDocument.Selection.NewLine(2) DTE.ActiveDocument.Selection.Text = "#endif /* " + filename + " */" DTE.ActiveDocument.Selection.NewLine() End Sub End Module 
+1
source

You can use a tool like autohotkey . Here is the answer to the simulated question.

0
source

If you have Visual Assist X, you can remove your #pragma once , if any, select the rest of the text, right-click and Surround with (VA) => #ifdef guard in a header . If you do not like the default value, you can override it in the VASSISTX menu and select Tools => Edit VA Snippets...

0
source

Source: https://habr.com/ru/post/946746/