>> Hello. My name is Kyle Reed and I work on
the C++ Language Services Team for Visual Studio.
I'm going to talk about three features of
Visual Studio that can improve
your productivity when working with C++.
Two of the features are new in Visual Studio version 2022
and the third is an older and experimental feature
that you may not have heard about.
First, let's look at the IntelliSense Linter.
This was an experimental feature in Visual Studio
2019 and in 2022 we've enabled it by default.
This feature helps you find and fix
common C++ problems while you're typing.
It uses the IntelliSense engine and
problems are reported nearly as fast as you type.
Results are displayed in the editor with underlines as
well as in the error list so you can filter and navigate quickly.
By default, messages are not
shown in the error list and you may need to enable them.
Results are also shown on the side margin in the overview
and the file health indicator can
navigate between errors and warnings.
When the cursor is on a squiggled tag,
you can see a short description of the error and clicking on
the lightbulb or using
the keyboard shortcut will open the list of suggested fixes.
You can see a preview of the fix before the change is made.
If you want to know more about an error.
you can click on the code in the error list
and it will take you to documentation about the warning.
To configure the linter, open Tools,
Options, Text Editor, C, C++.
Code Style, Linter.
Here, you can enable the linter and
change the severity of the checks.
In order to prevent overlapping results with
background code analysis we hide redundant results
preferring the linter result instead because
the fixes are available and they refresh more quickly.
Here's some examples of the supported checks.
We saw this one already as part of the demo but let's
just show that again.
You can see that this is a case where
an assignment is being used instead of equality so we
can change that to either equality or
inequality operator and you can see that the warning disappears.
Let's just look at a couple of examples that are
contrived just to show the point.
In this first example, the check simply
warns when a variable is declared but is uninitialized.
Core guidelines recommend that variables are
always initialized when they're declared.
You can see that the check is aware of
the type and provides an initializer that matches the type.
This next check looks for cases when integers are assigned
to a wider type after an arithmetic operation.
In this case, the multiplication operation.
It's possible with this sub-expression will
overflow before it is assigned to the wider type,
resulting in an overflow that is incorrect.
The fix there is to cast this sub expression to a wider type.
Another example is the flow truncation check
which looks for cases when
integer division is being
performed and then assign it to a floating point type.
Most of the time, what you typically want
is a floating point division to happen.
This is a suggestion which is to cast
one of the operands as a floating point type.
The unintended copy check looks for cases when
the use of auto may cause a copy that was not expected.
In this case, if we wanted to
enumerate the strings in the vector,
auto string makes a copy each time.
Likewise, when using the subscript operator,
it may be the case that you didn't want to make
a copy and just wanted a reference to it.
We saw this one above. This checks for
cases when in a Boolean context,
an assignment is made instead of comparison.
This check looks for cases when bitwise operators are used with
Boolean values and Boolean logic operators
are used with integer values.
You can see that these checks
run really quickly so if we uncomment these,
you can see how fast these checks are running.
In this case, we have
the Boolean and in this case we
expected the bitwise operator instead of the logical operator.
The comparison precedent check looks for cases
where bitwise operators are used
in conjunction with comparison operators.
The comparison operator has higher precedence and
so this part will be evaluated first.
Using parentheses to set precedence is required.
This next feature we've gotten a lot of
requests for under Developer Community.
It's called Inline Hints.
Inline hints appear inline in
the source code but are not part of it.
Because they're shown without needing to
hover over a symbol or invoke
some keyboard shortcut to show parameter help,
they are really helpful when reading code,
especially when the code is full of magic numbers.
Inline hints are easy to toggle on and off and so they are
there when you need them and
they are out of your way when you don't need them.
All you need to do is press Control twice to
toggle hints across all instances of Visual Studio.
When I'm using inline hints in my own code,
I'll usually toggle it off while I'm actively
writing but then toggle it back on when reading or browsing.
There are two flavors of Inline hints,
type hints and parameter hints.
Type hints will show the actual type
of a declaration that uses auto or decltype auto.
This is great when you're working in
unfamiliar code with lots of autos.
It can also help you spot errors when
the actual return type is not what you expected.
Finally, they can also help you spot errors in your code.
For example, while we were testing,
we found some game code where cosine x was
being passed in for a parameter called sine Theta.
There are a few options that can be configured for inline hints.
They are found under Tools, Options,
Text Editor, C++, IntelliSense.
Enable Inline Hints toggles the whole feature on and off.
Display Inline Hints toggles whether they're shown in the UI,
this can be toggled quickly using the keyboard shortcuts.
Then we can decide to enable or
disable type hints and parameter hints.
Parameter hints can optionally show the ampersand indicating
a non const reference parameter and we
also can hide hints that have an argument,
if the argument text matches the parameter name.
The argument text can contain comments.
Here's a function that shows inline hints very well.
Double tap ''Control'' to bring them
up and we can see the type ins,
Fill Position and Fill Length that show the actual type.
We can see that this function takes
its parameter by non const reference.
Here we see the parameter names.
There's also the option to hide hints which have
the same name as the argument. Here's an example.
Go to Tools, Options,
you can hide hints when the argument text
contains the parameter name.
It also works if
the comment contains the same name as the parameter name.
If your code has comments on magic numbers like this,
then it won't show a hint.
The final feature I want to talk about is Predictive IntelliSense.
This feature has been around for a while but it's been
hiding under the experimental page off by default.
One of the difficulties of working with C and
C++ is that there
are just so many symbols on the global namespace.
This can make looking for a name
that you're not sure about challenging.
Predictive IntelliSense can help with this by
using type information to filter the suggestion list.
For example, if you are
assigning to a variable and we know its type,
we'll try to only show completions with results
with types that can be converted to the variable's type.
In this case, we have a float,
so we open the suggestion list and we can see
that the only results
are things that could be assigned to a float.
In this case, I have a function called get_float and you can
see that just typing ge gets me get_float.
Likewise for string. I can type,
get_string and there it is.
The feature also looks at
parameter types when suggesting completions for arguments.
For example, I have a function called use_float,
that takes a float.
If I open the member list to look for suggestions,
I can actually filter to locals and fields and there's the float.
Same thing for string, Alt B and there's the string.
If you want to see all results in a list,
you can use Control J to show the entire list.
For example, use_float.
In this case, I want to use the object s.
You can see it's not in the list but if I hit Control J,
it will show up and now I can complete.
If you want to try out predictive IntelliSense,
you can find it under Tools, Options,
Text Editor C, C++ Experimental,
and check the Enable Predictive IntelliSense box.
As always, we want your feedback.
If there's something you love or something you don't love,
please let us know on Developer Community.
Your input helps us plan the future of Visual Studio. Thanks.