Lightning talk #7 – C++ functionality: Did you know… ?

lightning talk

Lambda under the hood

What if we capture by reference instead? [&] vs [=]

Lambda template

Move cannot move if const

C++17: Clamp

Before C++17, we were relying on the following code (or similar) to clamp a value.

Now, with C++17, we can simply use std::clamp.

Reverse Unique Sort Algos – With forward iterators

Warning: Unique works only with sorted values. If the container is not sorted, you will still have non-unique values inside.

Swap (C++11) vs Exchange (C++14)

Swap

  • Swaps two objects
  • Returns void

Exchange

  • Swaps (moves) two objects
  • Returns the old value

std::call_once (C++11)

  • Executes the Callable object f exactly once, even if called concurrently, from several threads.

std::iota (C++11) vs std::fill

std::iota

  • Fills the range [first, last) with sequentially increasing values, starting with value and repetitively evaluating ++value.

std::fill

  • Assign the given value to the elements in the range [first, last)

Atomic store/load and assignment

Direct assignment

  • This operation is atomic and uses sequential consistency (memory_order_seq_cst)
  • To modify the value with a different memory ordering, see atomic::store

Store / Load

class functions

The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.

Compiler intervention

Before

After

What was that?

Argument dependent lookup (Koenig lookup)

Floating point approximation

0.1 + 0.2 = 0.3 ?  Nope. 

0.1 + 0.2 = 0.3000000000000000000004

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

  • Decimal numbers cannot accurately represent a number like 1/3, so you have to round to something like 0.33 – and you don’t expect 0.33 + 0.33 + 0.33 to add up to 1.

Not always correct! But it’s a start!

Leave a comment

Your email address will not be published.