Hello everyone, I'm 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 I'm 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 that's built into the compiler,
our clang-tidy support and the new C++ linter. So let's get started. Okay,
so the first thing that I want to show you is how concepts IntelliSense works in the IDE.
In case you're 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 that's 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 don't
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 that's what I'm going to be showing you here today.
Now one of the first things that you're going to need to do if you want to use concepts in Visual
Studio is you're going to need to make sure that you've enabled C++20 for the project that you're
working on, and that's really easy to do. You just go to your project properties here and it's on the
first page under general. Go down here to pick your C++ language standard and you're going to
want to make sure that you're either using C++20 or the latest preview features. The latest preview
features might have things that aren't in the standard yet. So I'm 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 that's how you do this for an MSBuild project, but everything that I'm going to
show you also works for CMake projects in Visual Studio; you just need to make sure
that you've 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 I'm going to do next is I'm going to write some a templated function that uses this concept.
Okay and if you've ever written a lot of templated code in C++ before you might know that you don't
always have a fantastic IDE experience when you're editing it, and the reason for that
is the ide doesn't necessarily know how that template's going to be used. So one of the ways
that we've helped with that in the past is with our template IntelliSense feature, and that's
what this bar is that's just popped up here, and what that lets you do is it lets you specify a
particular type that's going to be going into that template what it's 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 don't actually need to do any of
that because the compiler already knows what you're 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, let's give the cat treats and give it a quick pat on the head.
And this is a really simple example. There's 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 they're 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 you're 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 you're writing C++ code, and identify some of the more critical
code safety or performance issues that might creep into your code as you're 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; I've accidentally done an assignment in an if statement and I'm sure we've 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 it's fixed it. And one of the things you might have noticed is those
scribbles on this went away almost immediately. If you've used our code analysis in the past,
you might know it brings the error, the code analysis errors and warnings into the editor,
but it's 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 you're 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, you'll
notice if I make an edit to this it's going to catch that almost immediately. Now the C++ linter
isn't really intended to replace code analysis, it's 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 you're 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 you're 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 haven't seen them already. So the two videos
I would recommend taking a look at would be Sy's 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 you're interested in how C++20 is implemented in both the compiler
and our libraries, I would recommend checking out
Mahmoud's video for a deeper dive into that as well. And of course I hope we see all of
you at Pure Virtual C++, it's on May 3rd. I look forward to seeing you all there!