C++ Productivity Improvements in Visual Studio 2019 Preview 2
Visual Studio 2019 Preview 2 contains a host of productivity features, including some new quick fixes and code navigation improvements:
- Quick fixes for:
- Quick Info on closing brace
- Peek Header / Code File
- Go to Document on #include
The Quick Actions menu can be used to select the quick fixes referenced below. You can hover over a squiggle and click the lightbulb that appears or open the menu with Alt + Enter.
Quick Fix: Add missing #include
Have you ever forgotten which header to reference from the C++ Standard Library to use a particular function or symbol? Now, Visual Studio will figure that out for you and offer to fix it:
But this feature doesn’t just find standard library headers. It can tell you about missing headers from your codebase too:
Quick Fix: NULL to nullptr
An automatic quick fix for the NULL->nullptr code analysis warning (C26477: USE_NULLPTR_NOT_CONSTANT) is available via the lightbulb menu on relevant lines, enabled by default in the “C++ Core Check Type Rules,” “C++ Core Check Rules,” and “Microsoft All Rules” rulesets. To change rulesets in a MSBuild project, navigate to Property Pages > Code Analysis > General; for projects using CMake, add the
"codeAnalysisRuleset" key into your CMakeSettings.json with the value set to the full path or the filename of a ruleset file.
You’ll be able to see a preview of the change for the fix and can choose to confirm it if it looks good. The code will be fixed automatically and green squiggle removed.
Quick Fix: Add missing semicolon
A common pitfall for students learning C++ is remembering to add the semicolon at the end of a statement. Visual Studio will now identify this issue and offer to fix it.
Quick Fix: Resolve missing namespace or scope
Visual Studio will offer to add a “using namespace” statement to your code if one is missing, or alternatively, offer to qualify the symbol’s scope directly with the scope operator: Note: we are currently tracking a bug with the quick fix to add “using namespace” that may cause it to not work correctly – we expect to resolve it in a future update.
Quick Fix: Replace bad indirection operands (* to & and & to *)
Did you ever forget to dereference a pointer and manage to reference it directly instead? Or perhaps you meant to refer to the pointer and not what it points to? This quick action offers to fix such issues:
Quick Info on closing brace
You can now see a Quick Info tooltip when you hover over a closing brace, giving you some information about the starting line of the code block.
Peek Header / Code File
Visual Studio already had a feature to toggle between a header and a source C++ file, commonly invoked via Ctrl + K, Ctrl + O, or the right-click context menu in the editor. Now, you can also peek at the other file without leaving your current one with Peek Header / Code File (Ctrl + K, Ctrl + J):
Go to Document on #include
F12 is often used to go to the definition of a code symbol. Now you can also do it on #include directives to open the corresponding file. In the right-click context menu, this is referred to as Go to Document:
Other productivity features to check out
We have several more C++ productivity improvements in Preview 2, covered in separate blog posts:
- Template IntelliSense Improvements
- In-editor Code Analysis
- Introducing the New CMake Project Settings UI
- Code analysis new rules: Concurrency, coroutines & move after free, and Lifetime Profile update
We want your feedback
We’d love for you to download Visual Studio 2019 and give it a try. As always, we welcome your feedback. We can be reached via the comments below or via email (firstname.lastname@example.org). If you encounter problems with Visual Studio or MSVC, or have a suggestion for us, please let us know through Help > Send Feedback > Report A Problem / Provide a Suggestion in the product, or via Developer Community. You can also find us on Twitter (@VisualC) and Facebook (msftvisualcpp).
About Quick fix. It would be a twice as quick if I didn’t have to click twice.