VS Code C++ Extension January Update: Create Definitions and Declarations

Alexandra Kemper

Starting with the 1.13.6 version of the C++ Extension in VS Code, we are happy to share a much requested feature: Auto creation of definitions or declarations for functions! You can now quickly create a declaration in a header file for a function you only have a definition for, or vice versa. These generated definitions and declarations already include all function arguments, so no need for constant copying, pasting, and double checking.

Let’s see it in action:  From a header file, a definition is clicked using a code action, which creates the declaration in a separate file. In that new file, a definition is added, which then is clicked via code action to invoke a declaration being added to the header file.

How can I create a Definition or Declaration?  

You can either: 

  • Click on your class function definition, then the Code Action (lightbulb icon) on the left. This will open a dropdown where you can select “Create a Declaration”. For creating a definition, it is the same process, just click on the function declaration, then select the Code Action for “Create a Definition” for your function. Clicking on the function, a lightbulb will appear on the left, where you can select the Option to "Create a Definition"/Declaration, depending on what you need.
  • Right click a function’s declaration or definition and select the “Create Definition/Declaration” from the context menu. A definition or declaration will be created depending on what is needed. Right click a function’s declaration or definition and select the “Create Definition/Declaration” from the context menu. A definition or declaration will be created depending on what is needed.
  • Select the function, then use the Command Palette (Ctrl + Shift + P) and type in the command Create Declaration/Definition.  

Select the function, then use the Command Palette (Ctrl + Shift + P) and type in the command Create Declaration/Definition.  

 

How do I know where the definition or declaration will be created?  

Definitions and declarations will be created based on the patterns you have already established in your code. For example, if you previously had added definitions and declarations in the same file, we will also add any new ones to that same file. Note that we will maintain function order automatically, so if your definition is between the functions Addition() and Subtraction() in Math.cpp and ask for a declaration, we will create the new declaration between the declarations of Addition() and Subtraction().  

This shows that when the class is already defined in the same file, invoking the create declaration command will declare a function in that same file.

Otherwise, if you have defined declarations or definitions in a different file from your source file, we will follow that convention.  In the common case of having a header and a source file with matching names, we will identify the other file, even if the header file has not been included in the source file. In this case, once the declaration or definition has been added, we will automatically include your header file to your source file for you.  

If you do not have any precedent of where you create definitions or declarations, we will create a header or source file with a matching name to your current file for you.  

How does this work for namespaces and templates?  

The functionality is the same for namespaces as classes. For templates, if a function template is declared in a header file, the definition of that function template will be created in the same header file, as shown below. This also applies for non-template member functions of class templates.  

 For templates, template functions will be defined in the same file when the feature is called, even if you have already defined/declared other functions in other files.

 

What do you think? 

Download the C++ extension for Visual Studio Code  today, give it a try, and let us know what you think. 

If you have any questions, feel free to start a discussion in our Github repository, otherwise if you run into any issues please report them in the  issues section. We can be reached via the comments below or in email at  visualcpp@microsoft.com. You can also find our team on Twitter at @VisualC. 

 

Posted in C++

0 comments

Discussion is closed.

Feedback usabilla icon