C++20 Cheatsheet (with examples)
Tags: programming, cpp, cpp20
Published: 2020-01-28 08:13:03

---
title: C++20 Cheatsheet (with examples)
published: true
description: C++20 is almost ready! See this compact reference card to have an overview of the new features.
tags: programming, cpp, cpp20
---

![C++20 Reference Card](https://dev-to-uploads.s3.amazonaws.com/i/gz5ng58aennjtjzfnhvp.png)

While the C++20 Standard is still being finalised and polished we know all of its core features. At first, the new specification of the language might sound complex and overwhelming. That's why, if you want to have an overview of the core elements and get the bigger picture, you can have a look at my new reference card.

## Want your own copy to print?

If you like, I prepared PDF I packed both language and the Standard Library features. Each one has a little description and an example if possible. 

![](https://4.bp.blogspot.com/-BtcL95O-FVU/Xi6WSTnfAsI/AAAAAAAAELU/qI0YEv6KTKUeL0v7BU4VoUR4uWlRVfB2QCLcBGAsYHQ/s1600/2017cards.PNG)

Here's what the text covers:

* Concepts
* Modules
* Coroutines
* operator `<=>`
* Designated Initializers
* Range-based for with initializer
* `char8_t`
* New attributes
* Structured Bindings Updates
* Class non type template parameters
* `explicit(bool)`
* `constexpr` Updates
* `consteval`
* `constinit`
* `std::format`
* Ranges
* Chrono Library Updates
* Multithreading and Synchronisation
* `std::span`
* and many others

All of the existing subscribers of my mailing list have already got the new document, so If you want to download it just subscribe here:

[Download a free copy of C++20 Ref Card!](http://eepurl.com/cyycFz)

Please notice that along with the new ref card you'll also get C++17 language reference card that I initially published three years ago. With this "package" you'll quickly learn about all of the latest parts that Modern C++ acquired over the last few years.

Let's now go through some of the core parts of C++20

## Language Features

### Concepts

Constrains on the template parameters and meaningful compiler messages in a case on an error. Can also reduce the compilation time.

```cpp
template <class T>
concept SignedIntegral = std::is_integral_v<T> &&
                         std::is_signed_v<T>;
template <SignedIntegral T> // no SFINAE here!
void signedIntsOnly(T val) { }
```

Also with terse syntax:

```
void floatsOnly(std::floating_point auto fp) { }
```

(constrained `auto`)

### Modules

The replacement of the header files! With modules, you can divide your program into logical parts.

```cpp
import helloworld; // contains the hello() function
int main() {
   hello(); // imported from the “helloworld” module!
}
```

### Designated Initializers

Explicit member names in the initializer expression:

```cpp
struct S { int a; int b; int c; };
S test {.a = 1, .b = 10, .c = 2}; 
```

### Range-based for with initializer

Create another variable in the scope of the for loop:

```cpp
for (int i = 0; const auto& x : get_collection()) {   
    doSomething(x, i);   
    ++i; 
}
```

### Attributes

* `[[likely]]` - guides the compiler about more likely code path
* `[[unlikely]]` - guides the compiler about uncommon code path
* `[[no_unique_address]]` - useful for optimisations, like EBO
* `[[nodiscard]]` for constructors – allows us to declare the constructor with the attribute. Useful for ctors with side effects, or RAII.
* `[[nodiscard("with message")]]` – provide extra info
* `[[nodiscard]]` is also applied in many places in the Standard Library

### consteval

A new keyword that specifies an immediate function – functions that produce constant values, at compile time only. In contrast to `constexpr` function, they cannot be called at runtime.

```cpp
consteval int add(int a, int b) { return a+b; }
constexpr int r = add(100, 300);
```

### Others

plus many more like coroutines, constinit, CTAD updates and more!

## Library Features

### `std::format` 

Python-like formatting library in the Standard Library!

```cpp
auto s = std::format("{:-^5}, {:-<5}", 7, 9);
```

`s` has a value of „--7--, 9----” centred, and then left aligned
Also supports the Chrono library and can print dates.

### Ranges 

A radical change how we work with collections! Rather than use two iterators, we can work with a sequence represented by a single object.

```cpp
std::vector v { 2, 8, 4, 1, 9, 3, 7, 5, 4 };
std::ranges::sort(v);
for (auto& i: v | ranges:view::reverse) cout << i;
```

With Ranges we also get new algorithms, views and adapters.

### `std::span` 

A non-owning contiguous sequence of elements. Unlike string_view, span is mutable and can change the elements that it points to.

```cpp
vector<int> vec = {1, 2, 3, 4};
span<int> spanVec (vec);
for(auto && v : spanVec) v *= v;
```

### Others

joining thread, semaphores, latches and barriers, Chrono library updates with calendar and timezones, `source_location`, `erase`/`erase_if` container functions, and many more!

## Summary

I hope with this concise reference card it will be easier to understand the scope of the new Standard.

Do you like the new features of C++20? What's your favourite part? Let us know in comments.

Once again, if you want to have a nice-looking pdf, just grab it from here:


[Download a free copy of C++20 Ref Card!](http://eepurl.com/cyycFz)