Quick Tips for using Copilot Chat with C++ in Visual Studio
GitHub Copilot Chat is now available in both Visual Studio and Visual Studio Code when you have an active subscription to the service. Copilot Chat is your AI pair-programmer that allows you to speak in plain text through the chat interface to accomplish programming tasks such as understanding sections of code, generating test cases, and generating code.
This blog post will focus on tips for using Copilot Chat functionality with C++ in Visual Studio, but the same functionality is available in Visual Studio Code and will be covered in future blog posts. Additionally, in future posts, we will dive into various C++ scenarios as reported by our customers.
You can learn more about the Copilot Chat extension through the Visual Studio documentation.
Download GitHub Copilot Chat
To learn more about accessing GitHub Copilot Chat in Visual Studio, please view these instructions. You will need a subscription to GitHub Copilot for Individuals or GitHub Copilot for Business. You will also need both the Copilot and Copilot Chat extensions installed for autocomplete-style suggestions and the chat-based features respectively.
Introduction to Copilot Chat in Visual Studio
You can use the new Copilot Chat functionality to work with your existing C++ code. To open the chat window, navigate to View > GitHub Copilot Chat or right-click anywhere in your code and select Ask Copilot.
Performing actions via / commands
In the chat window, you can use the / symbol to list out commands to perform on the codebase. The full list of available commands can be found here, or the list of commands will automatically populate the available commands when you type / in the chat window.
For example, you can use /explain to get code explanations on a desired function or file. You can also use /generate to ask Copilot Chat to generate a specific snippet of code.
Adding file context via # sign
Large language models (LLMs) are trained on public repository data at a point in time and do not know anything about your current codebase. You can use the # commands to specify the context you want Copilot Chat to include and help Copilot Chat provide contextually relevant answers. For example, you can specify #file:FIleName.cpp in-line in your prompt to Copilot Chat so that it looks at the specified file. Once you type the # sign, all file names or selections will populate to select.
By selecting a specific context, you can provide more information to Copilot Chat without having to copy/paste code and ensure you receive the most relevant answers when applicable. Copilot Chat will always indicate which files were included as context under References under your prompt. For example, by default, there are no references sent from my code upon no file specification.
Then, once some file is specified, you can see the code references in the References flyout.
As you’ll see, Copilot Chat can empower you as a C++ developer to focus more on the task at hand by being a true AI pair programmer through help with asks such as:
- Making code more readable
- Generating functions
- Generating test cases
- Understanding what a snippet of code does.
Making code more readable
Copilot Chat can improve your existing C++ code, as you can ask it prompts like to make your code more readable or simplify your existing code.
Here, there is some C++ code (starting repo linked here) that creates a Calculator class and has operations to add, subtract, multiply, and divide. This code is not as simple as it could be and creates some excessive variables. You can highlight the section and right-click to Ask Copilot to simplify this code for you. Copilot Chat will take the context in the highlighted section to propose better C++ code for your project. Since Copilot Chat operates as a pair programmer, Visual Studio will provide a diff view to allow you to accept, modify, or cancel the proposed changes from Copilot Chat.
Copilot Chat can also help you fix functions. For example, there are a few issues with this C++ code. Firstly, dividing by zero returns zero, which is not quite accurate. Instead, you may want it to return an error such as Not-A-Number (NaN). Therefore, you can ask Copilot Chat to rewrite the code such that it returns NaN and insert it into the file.
Additionally, these functions in the calculator do not change the Calculator state, so you can have Copilot Chat fix this issue at large by appending static modifiers to member functions that do not change the Calculator state. Copilot Chat will even provide helpful commentary on static functions.
Generate new C++ code
Copilot Chat can also help generate new code for you.
You can generate code by asking Copilot Chat in the chat window using the /generate command and specifying the specific file to generate code for. In this case, you can ask it to create a new function that calculates if two numbers are equal, and it will even use your existing coding conventions to generate a new function in your class.
Create tests for your code
Now that there are a few functions written for the calculator, you can ask Copilot Chat to create tests for this. Specifically, you can ask by using the /tests command in the Copilot Chat window to write out tests for my code. In this case, the output was GTests that follow the mathematical conventions expected and included the appropriate header functions.
Giving Copilot Chat additional context on your desired needs can yield better responses tailored to you. For example, if you did not want it to generate tests using the GTest framework, but instead wanted to use Catch2, you should explain that in your prompt. You can always add other relevant additional project contexts to Copilot Chat based on your desired results, such as desired package manager, language standard, and testing framework.
Copilot Chat can also help explain C++ code in plain text while referencing the applicable symbols. For example, you can ask Copilot what a highlighted section of the new test code does, and it will explain it all to you in plain language to make it easier for you to digest.
Copilot Chat will even suggest follow-up questions to the snippet of code at the bottom with easy, thought-provoking access.
Copilot Chat can help you achieve new levels of productivity in your C++ workflows. We are working hard on integrating it even further with Visual Studio, so please let us know any suggestions you’d like to see to improve your workflows. Additionally, let us know what other types of content you’d like to see regarding use cases or additional information about Copilot Chat. We welcome all types of feedback on your experience with the product. The comments below are open, or you can find us via email at email@example.com or via Twitter at @VisualC. To open a bug, please see Visual Studio feedback. You can also always upvote or downvote the provided suggestion in Copilot Chat to provide feedback to the model itself.