January 30th, 2025

Document build instructions and more with enhanced C++ awareness from Copilot Chat in Visual Studio

Sinem Akinci
Product Manager II

C++ development often involves nuances beyond the file you’re developing in. This can leave developers with many mental tabs open, balancing all the relevant context for their project to develop their C++ code properly.

Copilot Chat in Visual Studio can already utilize context provided by specified files and invoked agents such as @workspace to generate suggestions. However, to improve the relevance of Copilot suggestions, we must provide tailored context to Copilot with considerations specific to the C++ language for developers to achieve better results. That way, developers can focus less on providing all the information necessary for their questions and focus more on the task at hand.

Visual Studio can now provide Copilot Chat with various levels of C++ project context. These updates are designed to help C++ developers incorporate more relevant suggestions into their code, faster than ever.

To showcase Copilot’s increased awareness of C++, we will walk through 3 sample C++ workflows where Chat can provide assistance:

  1. Improving documentation for build instructions
  2. Generating new tests
  3. Modernizing C++ code

Upgrade to the latest version of Visual Studio 2022 and try out GitHub Copilot (now with plans available for free) to access these updates for C++.

Documenting Build Instructions

C++ projects can sometimes require developers to undergo several steps to be able to configure a project for development before they are even able to begin coding. Due to these requirements, it is important to document relevant instructions for onboarding developers thoroughly.

We asked Copilot Chat to improve documentation in the open-source project Endless Sky’s README. Copilot was able to include relevant context, such as dependencies like Ninja, to enhance Chat suggestions for improving documentation specific to this project’s needs, minimizing manual toil to onboard developers onto a project.

Copilot providing suggestions to add relevant configuration instructions to endless sky's readme file

Generating Additional Tests

Writing unit tests for C++ can be cumbersome and painstaking. With the /tests command, Copilot can assist with generating unit tests for the selected code. Copilot now has awareness of the available testing structure found in your project, so that when you invoke /tests to generate tests in your projects, friction is minimal for integrating the generated tests that fit in with your existing ones.

For example, when we prompted to generate new unit tests for the Step method in the Weather.cpp file in endless-sky using the /tests command, Copilot generated new tests that resemble the existing tests in the project:

Generating Unit Tests for the Weather.cpp file using Copilto provides Catch2 framework

When compared to existing test code, like test_ship.cpp, the Chat response includes the same relevant header files and test case structure like using SCENARIO and GIVEN for test cases.

Existing tests for test_ship.cpp with SCENARIO and GIVEN cases

Refactoring C++ code for modernization

Maintainability of C++ code can be difficult as C++ coding conventions evolve over the years. C++ developers often need to refactor legacy code to improve code clarity and maintainability over time.

Now, when asking Copilot questions related to C++ development such as refactoring code,  Copilot has the awareness to not only provide suggestions that are relevant to your selected code, but also suggestions that are compliant with your project’s configurations and standards.

Let’s take a look at an example, using some loop code that finds the greatest even element in a given container below. It can be useful to update C++ raw loop code to utilize algorithms instead for a myriad of reasons, including improving code readability and minimizing errors

Raw loop code in C++

When initially asked to refactor the code to incorporate algorithms or ranges using older C++/CMake versions, Copilot suggests using std::max_element to improve the code to be more concise instead of requiring a manual loop.

Copilot is refactoring the raw loop code to use the algorithm header in the original implementation

After updating the standard of the project to the latest standard of C++23, Copilot responses now include newer C++ features that can be relevant to the question, such as std::ranges, to improve the code.

Copilot refactoring the raw loop to use ranges with the latest C++ standard

What do you think?

Try out the latest Chat improvements for your C++ workflows today. To access these updates to Copilot, you’ll need an active GitHub Copilot subscription and the latest version of Visual Studio.

In addition, our team is working hard on improving C++ integrations with Copilot, so please let us know any other enhancements you’d like to see to your C++ workflows and content you’d like to see. Comment below, or you can find us via email at visualcpp@microsoft.com or via X at @VisualC.

 

 

 

Author

Sinem Akinci
Product Manager II

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

0 comments