CppCon 2016: Using Types Effectively—Ben Deane

Have you registered for CppCon 2017 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2016 for you to enjoy. Here is today’s feature:

Using Types Effectively

by Ben Deane

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

C++ has a pretty good type system, and modern C++ gives us a greater ability than ever before to use that type system for good: to make APIs easier to use and harder to misuse, to make our datatypes more closely express our intent, and generally to make code safer, more obvious in function and perhaps even faster.

This is an interactive session - incorporating games played between presenter and audience, even - taking a look at choices available to us as datatype and API designers, and examining how a little knowledge about the algebra of algebraic datatypes can help. We'll see why std::optional and (hopefully soon) std::variant will quickly become an essential part of everyone's toolbox, and also explore how types can be used to express not just the structure of data, but also the behaviour of objects and functions.

Generate lambdas for clarity and performance—Björn Fahller

A nice way to avoid code duplication and gain in readability:

Generate lambdas for clarity and performance

by Björn Fahller

From the article:

Higher order functions, functions that operate on other functions or returns functions, are familiar to those who have had some experience with functional programming, but they often seems magical to those who have not. Some of those with experience of using higher order functions have a gut feeling that they are expensive to use and prefer to avoid them...

Sorting by indices, part 2—Raymond Chen

It seems a simple problem, yet…

Sorting by indices, part 2

by Raymond Chen

From the article:

Before we dig into the Schwartzian transform, let's look at a more conventional generic way to sort by a key:

template<typename Iter, typename UnaryOperation, typename Compare>
void sort_by(Iter first, Iter last, UnaryOperation op, Compare comp)
  std::sort(first, last,
            [&](T& a, T& b) { return comp(op(a), op(b)); });

The idea here is that you give a unary operator op that produces a sort key, and we sort the items by that key according to the comparer...