The Practitioner's Guide to C++ 17/20

Background

  • C++ evolution defined by ISO committee
  • Being a member helps
  • Remember C++98 -> C++11? C++20 will be similar

Overview

  • Process
  • Big features and where they matter

Areas of Work

Evolution in several areas, in parallel. Several just missed C++17. Many major features ended up in C++20.

 

Features are proposals or Technical Specifications (TS)

C++ TSes

C++20 TSes

Big Items

  • Contracts
  • Concepts
  • Ranges
  • Modules
  • <=>
  • Coroutines
  • std::filesystem (C++17)

Small Items

  • span
  • format
  • optional + variant (C++17)
  • structured binding (C++17)
  • if constexpr (C++17)

  • if with variable declaration (C++17)

  • (string) literals as template parameters

  • more constexpr, consteval

  • class template argument deduction

Too much!

Little Helpers

if (auto v = f(); !v.get())
  • reduces scope of variable
  • extremely useful for things that are not true or false, but empty() or is_something()
if constexpr (is_enum_v<T>)
  • the contained block is NOT compiled!
  • needs to be parseable (not %$^&^&) but can be invalid!
  • extremely useful for avoiding template specializations
  • makes code MUCH more readable
MyTemplate<"a string">
  • people really wanted this
  • also allowed: any literal (="super simple") type with default <=> (see below)
auto [a, b] = getBoth()
  • finally a way to sanely deal with multiple values being passed
  • think if / for statements, where you can get key and value at once!
    • for (auto && [k,v] : myMap)

optional, variant

  • std::optional: holds one of none
  • std::variant: holds one of a set (union)
  • possibly also std::expected: value or error
  • extremely powerful for writing safe, compact code
variant<double, string> v{17.};
assert(get<double>(v) > 16); // good!
v.emplace<string>("ABC");
get<0>(v); // throws!

span

  • whether std::vector, std::array or C-style array:
  • refers to a contiguous array of given size
  • wonderful as function parameter
  • fixed-size or runtime-size
void func(span<double,4> lv);
array<double, 4> jetLV{...};
func(jetLV);

format

  • not yet guaranteed for C++20 - but expected!
  • an efficient and nice way to format strings in C++, finally
  • printf-format plus so much more
  • incl user-extensible: format your classes
string message = format("The answer is {}.", 42);

The
Big
Five

Contracts

  • specify what your function expects
  • can be checked by compiler
  • check can be turned off
  • enables optimizations

Setting expectations

expects: condition on arguments

ensures: post-condition, a guarantee by the function

assert: a check to be performed within the function

int f(int i) [[expects: i > 0]];
int g(string& s) [[ensures: !s.empty()]] {
   [[assert: s.empty()]]
   s = "ABC";
}

Validating expectations

Multiple contract levels default, audit, axiom

compiler flag defines which contracts are checked

- default checks default

- off: nothing

- audit: default and audit

axiom is thus never checked:

  good for expensive conditions

int f(int i) [[expects default: i > 0]];
int g(int i) [[expects audit: i > 0]];
int h(int i) [[expects axiom: i > 0]];

Concepts

  • document and restrict template parameters
  • better error messages

Ranges

  • working on everything that has a begin
  • generalized iterators
  • much, much nicer syntax

Modules

  • dramatic build time reduction
  • hides implementation details

Spaceship <=>

  • finally a default comparison!
  • reduces code clutter and bugs

Summary

  • C++20 is BIG
  • Implementations are on their way, most of C++17 already available
  • Get used to updating your compiler aggressively to benefit from C++
  • Goals are simplicity, 0-cost, faster programs, commonly used features in the library

Outlook

  • C++23 will be much smaller
  • implementing language features for library where needed
    • stdlib modules
    • more concepts
  • several major features did not make it:
    • networking
    • executors
  • more features in the works, e.g. reflection

Your C++ representative

(disclaimer: not my main job)

  • we have > 100 million lines of C++ code in HENP, replacing that might be more work than fixing C++
  • I will be at ACAT all week: share your ideas, pet peeves, thoughts about C++'s future role in HENP codes

Conclusion (1/2)

  • C++ learned a lesson: evolve or be dead
  • relevant to us: better, more maintainable code
  • we won't need all features, but we have a palette to select from
  • need to upgrade our toolset (compilers!) and coding guidelines: contracts? concepts?

Conclusion (2/2)

  • many features are targeted at us
    • math special functions (C++17)
    • ranges
    • concurrency
    • compile times of large-scale code
  • not using them means missing out on benefits provided by the language and its tools
  • we need to evolve tooling and code to benefit
  • are we investing enough in our code and education?

deck

By axeln

deck

  • 208