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.

Let there be constants!

In the beginning, there was const. And people saw that const was good. And then the people said: let there be constexpr, and consteval, and constinit. And thus, starts this article about constant functions and variables in C++20, which I will try to keep short and concise.

C++ is fun

We all know C++ is a powerful yet complex programming language. But it can also be fun. For instance, you can use emojis and other Unicode characters in the source code. Of course, nobody sane would use emojis for identifiers. But it’s possible and you can have a little fun if have some spare time.

Identifiers are sequences of digits, underscores, lowercase and upper case Latin letters, and most Unicode characters. Identifiers are not allowed to begin with a digit, they must begin with an underscore, a Latin letter, or a non-digit Unicode character. For more information about the Unicode characters in identifiers see this.

So here is a little example of a C++ program that uses emojis. Can you figure out what it does?

Enabling TLS 1.2 in your .NET framework applications

A functionality of one of the products I’m working on suddenly stopped working without any code changes on our side. Our application connects to a web service to get some data and that no longer worked, our customers getting the following error in their logs “The underlying connection was closed: An unexpected error occurred on a send.” The first thing to do was checking whether the web service was still up and running as expected. The requests made with SoapUI or Postman were all successful, so it was actually something in our application that was actually wrong. So I decided to use Fiddler to look at how our requests look and what do we get back.

Putting the fun in C++

The post-Kona mailing list of the new standards papers has been recently published (you can find it here). Going through the titles I could not help smiling because some of them are really funny. So I decided to make a top 10 of the funniest paper titles.

Little-known C++: operator auto

A user-defined conversion function enables an implicit or explicit conversion between types. Such, a function has the following form (no return type and no parameters):

struct foo
{
   operator int() const {return 42;}
};

foo f;
int i = f;  // implicit conversion

Little-known C++: function-try-block

Function-try-block is a mechanism in C++ to establish an exception handler around the body of a function. The following is an example:

int foo() 
{
   throw std::runtime_error("oops...");
}

int main()
try
{
   foo();
   return 0;
}
catch (...)
{
   return -1;
}

C++ code samples before and after Ranges

The Ranges library proposal has been accepted for C++20 at the San Diego meeting of the standard committee in November last year. The library provides components for handling ranges of values aimed at simplifying our code. Unfortunately, the Ranges library is not very well documented, which makes it harder to grasp for those that want to learn it. This post is intended as an introduction based on examples of code written with and without Ranges.