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:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.