1. Overview of C++20 and a
deeper look into modules
export module cpp20;
export bool doModulesRock()
{
return true;
}
2. Overview of C++20 and a deeper look into modules
Speaker
● Marius Meisenzahl
● Co-founder of
○ located in Bochum
○ we build software from embedded to web
○ we offer training for
■ testing
■ software architecture
■ DevOps
■ ...
2
/meisenzahl /meisenzahl
3. Overview of C++20 and a deeper look into modules
C++20
Core Language Library Concurrency
Four
The Big Five
● Three-way comparison
operator
● Strings literals as template
parameters
● constexpr virtual functions
● Redefinition of volatile
● Designated initializers
● Various lambda improvements
● New standard attributes
● consteval and constinit
keyword
● std=:source_location
● Calendar and
time-zone
● std=:span as a
view on a
contiguous array
● constexpr
containers such
as std=:string
and
std=:vector
● std=:format
● std=:atomic_ref<T>
● std=:atomic<std=:shared_ptr<T=>
and
std=:atomic<std=:weak_ptr<T=>
● Floating point atomics
● Waiting on atomics
● Semaphores, latches and barriers
● std=:jthread
● Concepts
● Contracts
● Coroutines
● Ranges
● Modules
3
4. Overview of C++20 and a deeper look into modules
Core Language > std=:source_location
● Provides access to file name, line number, ...
● Modern alternative to preprocessor macros like =_FILE=_ and =_LINE=_
● Prime candidate for custom logging functionality in combination with
std=:format
https://en.cppreference.com/w/cpp/utility/source_location
4
5. Overview of C++20 and a deeper look into modules
Library > std=:format
● https://github.com/fmtlib/fmt is integrated into C++
● Python like string formatting
● Define formatting rules for custom types
fmt=:print("Hello, {}!", "world");
std=:string s = fmt=:format("I'd rather be {1} than {0}.", "right", "happy");
5
6. Overview of C++20 and a deeper look into modules
Concurrency > std=:jthread
● A new kind of std=:thread
● Can automatically join
● Can be interrupted
○ std=:interrupt_token
https://www.modernescpp.com/index.php/a-new-thread-with-c-20-std-jthread
6
7. Overview of C++20 and a deeper look into modules
Contracts
int push(queue& q, int val)
[[ expects: !q.full() ]]
[[ ensures !q.empty() ]]
{
==.
[[assert: q.is_ok() ]]
==.
}
https://www.reddit.com/r/cpp/comments/cmk7ek/what_happened_to_c20_contracts/
7
Precondition
Postcondition
Assertion
8. Overview of C++20 and a deeper look into modules
Concepts
● Extension of templates
○ specify which semantic categories are allowed
● Templates can be used more expressive
● Check types during compile time
○ “is sortable”, “is equality comparable”, …
https://www.modernescpp.com/index.php/c-20-concepts-the-details
8
9. Overview of C++20 and a deeper look into modules
Coroutines
● Asynchronous programming
○ cooperative multitasking, event loops, ...
○ new keywords co_await and co_yield
==.
size_t n = co_await socket.async_read_some(buffer(data));
co_await async_write(socket, buffer(data, n));
==.
https://en.cppreference.com/w/cpp/language/coroutines
9
10. Overview of C++20 and a deeper look into modules
Ranges
● Apply algorithms of STL on containers
○ connect them with pipe operator
● Use infinite data streams
auto children_names =
all_people
| filter([](const auto& person) { return person.age < 14; })
| transform([](const auto& person) { return person.name; })
| to_vector;
https://vector-of-bool.github.io/2019/10/21/rngs-static-ovr.html
10
11. Overview of C++20 and a deeper look into modules
Modules
● Alternative to header and implementation files
○ can halve the number of files in the code base
● No more include guards and/or #pragma once
○ But you can also separate between interface and implementation
● Faster compilation times?!
○ “Reduce build times due to not reparsing large header files”
■ https://gcc.gnu.org/wiki/cxx-modules
○ http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1441r1.pdf
● Isolation of preprocessor macros
● Better way to express which parts of a library are exported
https://vector-of-bool.github.io/2019/03/10/modules-1.html
11
12. Overview of C++20 and a deeper look into modules
Modules > Names
module-name:
[module-name-qualifier] identifier ;
module-name-qualifier:
identifier "." | module-name-qualifier identifier "." ;
=> speech | speech.english
12
13. Overview of C++20 and a deeper look into modules
Modules > Declaration
module-declaration:
["export"] "module" module-name [module-partition] [attribute-specifier-seq] ";" ;
module-partition:
":" module-name ;
=> export module example;
13
14. Overview of C++20 and a deeper look into modules
Modules > Partitions and “Submodules”
Partitions
export module speech;
export import :english;
export import :spanish;
“Submodules” → Separate modules
export module speech;
export import speech.english;
export import speech.spanish;
14
15. Overview of C++20 and a deeper look into modules
Modules > Interfaces
hello.cppm
export module hello;
export const char *greet() { return "Hello, world!"; }
15
16. Overview of C++20 and a deeper look into modules
Modules > Interfaces and Implementations
hello.cppm
export module hello;
export const char *greet();
hello.cpp
module hello;
const char *greet()
{ return "Hello, world!"; }
16
17. Overview of C++20 and a deeper look into modules
Modules > What can be exported?
● namespace
● class
● struct
● non-static variables/constants
● ...
Understanding C++ Modules: Part 2: export, import, visible, and reachable
17
18. Overview of C++20 and a deeper look into modules
Modules > Interoperability
Every compiler uses it’s own binary format. You
can not interchange the precompiled modules.
https://www.reddit.com/r/cpp/comments/d5dxr3/do_c20_modules_have_a_st
andard_binary_format_if/
18
19. Overview of C++20 and a deeper look into modules
Compiler support
TL;DR: WIP
https://en.cppreference.com/w/cpp/compiler_support
https://clang.llvm.org/cxx_status.html
https://gcc.gnu.org/projects/cxx-status.html
https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conform
ance?view=vs-2019
19
20. Overview of C++20 and a deeper look into modules
Resources
● https://isocpp.org/
● http://www.open-std.org/jtc1/sc22/wg21/docs/papers/
● https://en.wikipedia.org/wiki/C%2B%2B20
● https://www.reddit.com/r/cpp/comments/cfk9de/201907_cologne_iso_c_committee_trip_report_the/
● https://docs.microsoft.com/en-us/cpp/cpp/cpp-language-reference
● https://en.cppreference.com/w/
● https://vector-of-bool.github.io/
● https://blog.panicsoftware.com/
● http://modernescpp.com/
20