C++20 Reference Card

栏目: IT技术 · 发布时间: 6年前

内容简介: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

C++20 Reference Card

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 which packs both language and the Standard Library features. Each item has a short description and an example if possible.

C++20 Reference Card

Here’s what the text covers:

<=>
char8_t
explicit(bool)
constexpr
consteval
constinit
std::format
std::span

Special thanks to the CppPolska team , Andrzej Krzemienski ( akrzemi1 ), Łukasz R., Yehezkel B. and many others from my mailing list - for valuable feedback about the features, typos and other improvements.

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!

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.

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.

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

Designated Initializers

Explicit member names in the initializer expression:

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

Range-based for with initialiser

Create another variable in the scope of the for loop:

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

Attributes

[[likely]]
[[unlikely]]
[[no_unique_address]]
[[nodiscard]]
[[nodiscard("with message")]]
[[nodiscard]]

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.

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!

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 in how we work with collections! Rather than use two iterators, we can work with a sequence represented by a single object.

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.

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.


以上所述就是小编给大家介绍的《C++20 Reference Card》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网产品运营:产品经理的10堂精英课

互联网产品运营:产品经理的10堂精英课

丁华、聂嵘海、王晶 / 电子工业出版社 / 2017-5 / 59

《互联网产品运营:产品经理的10堂精英课》共有10章,前9章分别从互联网产品运营的9个点入手,最后一章辅以案例,分析当下市场热门产品的运营模式。 第1章点明在运营产品之前需要经过缜密的策划,这样才能有明确的运营方向;第2章讲述产品运营的定位,有了准确的定位,运营才不会走偏;第3章描述用户运营,用户是一款产品的根本,没有用户,产品就是死的;第4章讲述内容运营的技巧,产品内容要怎么运营才能受到用......一起来看看 《互联网产品运营:产品经理的10堂精英课》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码