February 6th, 2024

Quick Tips for using Copilot Chat with C++ in Visual Studio

Sinem Akinci
Product Manager II

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.

An image of the Copilot Chat pane in Visual Studio

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.A list of all the slash commands available in Visual Studio once pressed, such as the /askvs command highlighted and a brief explanation to the right.

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.

Pressing the # sign in Copilot Chat populates all the documents available to specify to Copilot Chat.

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.

Copilot Chat References displaying no references were sent to the model when no file context is provided.

Then, once some file is specified, you can see the code references in the References flyout.

Image references nousername

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.

Please keep in mind that while GitHub Copilot Chat is a great tool for software developers to get started across a multitude of different tasks, it is powered by AI and surprises and mistakes are possible. Make sure to verify any generated code or suggestions provided in your scenarios.

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.

Some C++ code showcasing a Calculator class with functions such as add, subtract, multiply, and divide

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 simplifying some code as shown in a GIF.

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.

Asking Copilot to rewrite the divide function such that it returns NaN instead of 0.

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.

Asking Copilot Chat to make member functions static

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.

Asking copilot to generate a new function that checks if two numbers are equal and copilot chat generated code

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.

Asking copilot chat to create tests for your code creates GTest unit tests

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.

A prompt asking for Catch2 framework to create unit tests in main.cpp with Copilot giving the right response.Explain a section of code

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 explaining what a section of my test code does.

Copilot Chat will even suggest follow-up questions to the snippet of code at the bottom with easy, thought-provoking access.

Copilot Chat will automatically suggest follow-up questions to ask. In this case, it asked follow up questions about running my Google TestsWhat do you think?

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 visualcpp@microsoft.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.

 

Category
C++Copilot

Author

Sinem Akinci
Product Manager II

C++ Product Manager working on Copilot, CMake, and Linux experiences in Visual Studio and VS Code

5 comments

Discussion is closed. Login to edit/delete existing comments.

  • Hollman, David

    Does Copilot send any of your code or derived data outside of your PC?

  • Peter Nimmo · Edited

    Would it be possible to increase the size of the animated GIFs in future, the static GIFs are just fine but the animated GIF that shows proposed changes from Copilot Chat, is too small to see, and I have to Zoom in the page, to see it but then the text size is far too big.

    Also its not obvious whether you need to install both https://marketplace.visualstudio.com/items?itemName=GitHub.copilotvs and the extension talked about in this post.

    Originally I...

    Read more
    • Hollman, David

      Opening the GIF in a new tab was helpful in my case. That zoomed it in to fill the space.

      GIFs also don’t seem to be pause-able in Chrome.

    • Sinem AkinciMicrosoft employee Author

      Thank you for your feedback, I can surely increase the size of GIFs in the future.

      Right now, you should download both Copilot and Copilot Chat extensions in Visual Studio. The Copilot extension (the one you linked) provides autocomplete-style suggestions as you code, while the Copilot Chat extension is a companion to the Copilot extension and provides the Chat-based features shown above. The link to that extension is here, and I'll update the post: https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-chat

      Read more