C++20 Concepts in Visual Studio 2019 16.3 Preview 2

Back in mid-August, Microsoft released the 2nd preview of Visual Studio 2019 16.3. This is the first version of Visual Studio to support concepts from C++20 both in the compiler and the standard library (header <concepts>) without the changes made at the ISO C++ standards meeting in Cologne. These changes are available when you compile with the /std:c++latest switch.

Concepts allow performing compile-time validation of template arguments and function dispatch based on properties of types. Concepts are very useful in libraries where they can be used to impose compile-time checks on the template arguments of functions or types. For instance, a generic algorithm for sorting a container would require the container type to be sortable for the program to even compile.

In this article, I will show an example with a concept that verifies that a type T can be converted to a std::string via a to_string() function, that is either a member of the class or a free function.

In the code below, OverloadsToString is a concept that checks that a free function called to_string exists, and it takes a single argument of type T and returns a std::string. On the other hand, HasToString is a concept that checks that a type T has a method called to_string that takes no arguments and returns a std::string. These two concepts, are composed together using operator || in a new concept called StringConvertible. A concept composed this way is called a disjuction. On the other hand, a composition of two constraints with operator && is called a conjunction.

We can use the StringConvertible concepts to perform checks on types. In the following example, an instance of the class foo can be converted to a std::string using the function to_string (for simplicity, this function does not to much except returning the same string for all instances). For the class bar there is no way to convert an instance of it to a std::string. On the other hand, the class foobar has a method called to_string. We can verify that a type is satifsfying the concept or not using static_assert, as shown below:

As mentioned earlier, concepts are useful to express expectations on the template arguments of a function template or a class template. In the following example, serialize is a function template that expects its template argument to be convertible to a std::string. Similarly, Serializable is a class template that expects the template argument T to be convertible to a std::string.

If you compile this code, the lines marked with error (in the comments) will produce the following errors:

The syntax used above (template <typename T> requires StringConvertible<T>) to specify the expectations on the template argument is a bit cumbersome. An alternative that is simpler and more intuitive exists:

All the code shown here compiles with Visual Studio 2019 16.3 Preview 2.

Of course, this is barely an introduction to concepts. There are many more things you need to learn about concepts, which you can find on thw web. You can learn more about concepts here:

Three productivity features in the Visual C++ 2017 debugger

Visual Studio 2017 has had a larger number of updates throughout its lifetime. At the time of writing this article, there have been ten major updates and countless minor ones. You can check the release notes history for details. Part of the changes was in the debugger. In this article, I will mention three features for C++ development that were introduced at different stages in Visual Studio 2017.

Run to Click (Available since 2017 15.0.)
When you are in the debugger and you hover the cursor over a statement in the editor (or the Disassembly window) a green button appears. By clicking this button you resume the execution until that point. This functionality was already available either through a (temporary) breakpoint or by the Run to Cursor command. But with this contextual button, it is made much simpler.

See more details: Navigating through code with the Visual Studio debugger

Just my code (Available since 2017 15.8.)
This is a feature that avoids stepping into frameworks, system, and other non-user code. This is available for other languages too, but for C++ it requires the /JMC compiler switch (which is enabled by default for new projects).

Here is an example. Suppose you are in the debugger at this line:

Stepping into the function call, you expect to get here:

This is exactly what you get with this feature enabled. Without it, however, the debugger used to step into standard library code, as shown below:

This feature can be enabled from Tools > Options > Debugging > General by selecting Enable Just My Code. In Visual Studio 2019, this feature is enabled by default.

When using this feature, in order to have the code classified as user-code, the PDB for the image containing it must be available to the debugger. When debugging:

  • Step Into (or F11) on non-user code steps over the code to the next line of user code and Step Out (or Shift+F11) on non-user code runs to the next line of user code.
  • when there’s no more user code, debugging continues until it ends, hits another breakpoint, or throws an error.
  • when breaking in non-user code stepping continues in the non-user code.
  • when hitting an exception, it stops on the exception, whether it is in user or non-user code. User-unhandled options in the Exception Settings dialog box are ignored.

You can customize what code should be considered non-user (it’s an opt-out feature) by creating a *.natjmc file (in the %VsInstallDirectory%\Common7\Packages\Debugger\Visualizers folder if you want to change the behavior for all users, or in the %USERPROFILE%\My Documents\Visualizers folder for a particular user). Here is an example, from Microsoft Docs, on how such a file may look like (for details on the format, you should read the documentation):

See more details: Debug only user code with Just My Code

Step back (Available since 2017 15.9.)
This latest feature enables us to debug by returning to a previous point in time during the debugging without restarting the process. How is this done? Behind the scenes, the debugger uses PssCaptureSnapshot, a Windows API, to basically span a child process and take a snapshot of its memory. This is all hidden to the user, but when you step back, the debugging of the current process is actually stopped and the debugger is attached to one of these snapshot processes with access to its snapshot state (its entire virtual memory). When you return to live debugging, the debugger is reattached to the debugged process and proceeds in a normal manner.

Snapshots are taken each time a breakpoint is hit and at each step event, but not sooner than 300ms since the last one. This is done to ensure performance remains reasonable.

To make this work, you must first enable IntelliTrace Snapshots, from Tools > Options > IntelliTrace > General. (Notice that the following screenshots are taken in Visual Studio 2019 Preview, but they are basically the same in Visual Studio 2017.)

With this enabled, the Diagnostics Tool window changes to enable you to take snapshots. You will also see the snapshots that are taken automatically and you will be able to go through them and see the values of your variables in the Auto, Local, or Watch window.

You can also move back and forth through the snapshot history with the Step Backward and Step Forward commands from the debugging toolbar.

See more details: Step Back – Going Back in C++ Time

A first look at Visual Studio 2019

The first preview version of Visual Studio 2019 is available since the beginning of December 2018. I decided to take a look to see what is different from the previous Visual Studio 2017 version. In this post, I will present some of the things that are new or changed in this new version.

The first thing that I stumble upon was the icon that looks different. It’s a departure from the flat Windows 8 style and the resurrection of good old beautiful icons. Below you can see on the left the icon for the release version and on the right the icon for the preview version.

The next thing to notice is the way you can get started after launching Visual Studio; you can

  • open a recent project or solution
  • clone or checkout code from GitHub or Azure DevOps
  • open a project or solution
  • open a local folder
  • create a new project

The wizard for creating a new project has also changed. The new window enables better discoverability of templates. You can filter the templates with a search term but also by language (C++, C#, F#, VB, JavaScript, etc.), platform (Windows, Azure, Android, iOS, MacOS, Linux, etc.), and project type (desktop, console, cloud, UWP, test, etc.).

The new scenario that is enabled from the open window is to clone or checkout code from publicly available URL. This enables cloning repositories such as from GitHub or Azure DevOps. In this preview version, checkout is not supported. TFS repositories are also not supported. I am not sure what the plans are but I hope that in the release version TFS and perhaps other source control systems will be added (or at least supported through extensions).

The menu and toolbars have also slightly changed. The title bar is completely gone and other controls have been rearranged. There is a new button for Live Share. This is a collaborative service that was available in Visual Studio 2017 as an extension but is now built into Visual Studio 2019. It enables developers to collaborate in real time for editing and debugging code regardless of the programming language or application type. You can learn more about it here.

Another new feature in this version is called Document Health Indicator. It appears as a small icon on the bottom right corner of the opened document and when hovered shows a summary of errors, warnings, and suggestings for the current document. The status displayed in this pop out does not depend on you actually compiling the document; it is updated as you edit it.

For C++ documents, the only actions from this indicator (when you right-click on it) are navigating to the next and previous issue in the file. However, for other languages, such as C#, there are more options. In C# files you can also run code cleanup.

Code cleanup executes a series of actions to improve your code. Among these actions are sorting using statements and removing unnecessary using statements, removing unused variables or unnecessary casts, adding or sorting accessibility modifiers, etc. You can see the current list of actions in the following image. These can be individually selected and you can choose to apply them based on your last settings without selecting them again.

The Clipboard Ring feature, that shows the history of the clipboard copies, has been redesigned. It is now available with the shortcut Ctrl + Shift + V and presents the clipboard content in a more useful way.

There are much more changes to VS 2019 than this list of IDE features. If you want to read more about the upcoming Visual Studio you should read the following articles: