In Software Development
Modernizing C++ with Visual Studio with Will Buik - read the full article about C++ 2021, Software Development and from Microsoft Visual Studio on Qualified.One
Hello everyone, Im will and I am going to show you a few of the ways that Visual Studio can help you leverage some of the latest features of the C++ language standard and Im also going to show you some of the ways that Visual Studio can help you maintain code quality for both your existing code bases and new ones. So in particular what I want to show you are some of the ways that Visual Studio can help you leverage C++20 concepts and a few of the features that we have to help maintain code quality like code analysis thats built into the compiler, our clang-tidy support and the new C++ linter. So lets get started. Okay, so the first thing that I want to show you is how concepts IntelliSense works in the IDE. In case youre not familiar with them, concepts are a new feature in C++20 and at a high level what concepts allow you to do is to specify either a set of constraints that a type has to meet, or a set of behaviors that a type has to provide, and what you can then do with those concepts is you can use them in templated code and you know that anything that that template gets instantiated with is going to satisfy all of those constraints, and anything thats defined in the concept is going to be available for the code that you write in the template to use. The C++ standard library in C++20 does have a number of concepts built into it. They do a variety of things. As a couple examples, you have various kinds of iterators whether, or not types are incrementable, decrementable, are types sortable, and also certain properties of types, for instance is a type numeric. Now you dont just have to use one of these concepts at a time, you actually can build up chains of these concepts that, for instance, you can know something can be incremented and is sortable. Of course you also can define your own concepts and thats what Im going to be showing you here today. Now one of the first things that youre going to need to do if you want to use concepts in Visual Studio is youre going to need to make sure that youve enabled C++20 for the project that youre working on, and thats really easy to do. You just go to your project properties here and its on the first page under general. Go down here to pick your C++ language standard and youre going to want to make sure that youre either using C++20 or the latest preview features. The latest preview features might have things that arent in the standard yet. So Im going to pick C++20 here.
And once we do that the compiler is going to support all the C++20 features and IntelliSense is going to support them as well in the IDE. Now thats how you do this for an MSBuild project, but everything that Im going to show you also works for CMake projects in Visual Studio; you just need to make sure that youve appropriately set the language standard in your project CMakeLists.txt file. So I am going to open this source file right here and we can see that we have a really simple concept defined in it, is_cat and it specifies a couple of the behaviors that a cat is expected to have. So you can pet the cat, give it a treat, and being a cat of course it can sleep as well. So what Im going to do next is Im going to write some a templated function that uses this concept.
Okay and if youve ever written a lot of templated code in C++ before you might know that you dont always have a fantastic IDE experience when youre editing it, and the reason for that is the ide doesnt necessarily know how that templates going to be used. So one of the ways that weve helped with that in the past is with our template IntelliSense feature, and thats what this bar is thats just popped up here, and what that lets you do is it lets you specify a particular type thats going to be going into that template what its going to be instantiated with and then IntelliSense will provide all of the member lists and recommendations based on that type and this is actually, it has another useful feature; it can actually scan through your whole solution and recommend anything that has been used to instantiate that particular template. But the really cool thing with concepts is you dont actually need to do any of that because the compiler already knows what youre going to be putting into that template; it has to satisfy the concept. So if I start typing here without specifying anything we can see our member list has all of the member functions that are defined on this concept because we know that the type is going to satisfy all of those constraints. So I can just start typing here, lets give the cat treats and give it a quick pat on the head.
And this is a really simple example. Theres not a lot in the cat template, we just have these three member functions, but once you start taking dependencies on many concepts at a time, or more complex data types this can really help you streamline the code, streamline the process of writing your templated code, and it can also make sure that your templates are more robust and give more accurate error errors when theyre instantiated incorrectly.
Changing gears a little bit, one of the next things that I want to show you are some of the ways that Visual Studio can help you maintain the high code quality. Visual Studio has a number of features that can help you out with this both as youre writing new code bases and to identify potential issues in existing ones. So what I want to show you right now is the new C++ linter in Visual Studio. So I will open this up now. What the C++ linter is really trying to do is to find some of the most common typos that you can make when youre writing C++ code, and identify some of the more critical code safety or performance issues that might creep into your code as youre writing it. So just to give you a quick demonstration of this, down here I have made one of the quintessential typos in C++ code base; Ive accidentally done an assignment in an if statement and Im sure weve all done this, and we can see that the linters identified this issue and it actually even provides a fix right here, if I click on this light bulb I can actually just get either a equality or inequality and click on this and its fixed it. And one of the things you might have noticed is those scribbles on this went away almost immediately. If youve used our code analysis in the past, you might know it brings the error, the code analysis errors and warnings into the editor, but its not quite happening as you type because these issues are being surfaced by the compiler, it does have to hand off the translation unit that youre working on to the compiler, which does take a little bit of time to actually surface the issues in the ide. The new C++ linter, youll notice if I make an edit to this its going to catch that almost immediately. Now the C++ linter isnt really intended to replace code analysis, its meant to augment it. If you need more complex rule sets than the linter provides, we really do recommend that what you use for that is either our code analysis or clang-tidy support and all of that is surfaced in both the error list and in the IDE editor, but this linter can really be helpful as youre editing code to provide immediate feedback for potential issues and mistakes before they even creep into your code base at all.
Okay, so that about wraps up what I wanted to show you in Visual Studio. If youre interested in learning a little bit more about how C++20 works in Visual Studio we actually have a couple other demo videos that you might be interested in if you havent seen them already. So the two videos I would recommend taking a look at would be Sys whirlwind tour of C++20 in visual studio video, and that goes into ranges, concepts, modules, and coroutines and looks at how those work in the IDE, and if youre interested in how C++20 is implemented in both the compiler and our libraries, I would recommend checking out Mahmouds video for a deeper dive into that as well. And of course I hope we see all of you at Pure Virtual C++, its on May 3rd. I look forward to seeing you all there!
Microsoft Visual Studio: Modernizing C++ with Visual Studio with Will Buik - Software Development