December 2013

Nugget: C++1y Automatic Type Deduction—Tony DaSilva

A short nugget by Tony DaSilva:

C++1y Automatic Type Deduction

by Tony DaSilva

From the article:

In addition to this convenient usage, employing auto in conjunction with the new (and initially weird) function-trailing-return-type syntax is useful for defining function templates that manipulate multiple parameterized types (see the third entry in the list of function definitions below).

Type Erasure, Part 3—Andrzej Krzemieński

In part 3, Andrzej turns to some practical use cases for type erasure.

Type Erasure, Part 3

by Andrzej Krzemieński

From the article:

Who needs type erasure?

... Here is what I learned from my experience. There are a couple of trade-offs to be made when using or not type erasure: run-time efficiency vs compilation time, run-time efficiency vs binary size. The choice is not obvious. Even in programs that need to be fast, not every part of the program needs to be fast. Some portions, like the interaction with the user, can be slow, and for these parts you can apply different trade-offs.

static_assert: Better template error messages—Michael Price

This article could be subtitled, and ends with, "Why we need Concepts."

Here's a quick take on how far you can get with just <type_traits> and static_assert, and why you might want to do that.

static_assert: Better template error messages

by Michael Price

From the article:

Templates are one of the most powerful features of C++, and provide the language with a serious advantage over many of its brethren.  But templates have serious drawbacks as well, one of which is the incredibly verbose and dense error messages that are provided should you fail to provide the right kind of parameter to a templated entity.

Type Erasure, Part 2—Andrzej Krzemieński

In part 1, Andrzej explained what "type erasure" is all about. But how do you create custom type-erased interface?

Type Erasure, Part 2

by Andrzej Krzemieński

From the article:

While there are many ways to erase a type, I will use name type erasure to denote the usage of std::function-like value-semantic interface. This convention appears to be widely accepted in C++ community.

... Using std::function is easy, but this is because someöne has made an effort to implement it for us. So let’s try to see how much effort it is to write our own type-erased interface. We will be using countdown counters: something that (1) can be decremented and (2) tested if the count reached zero.

The Cost of Dynamic (Virtual Calls) vs. Static (CRTP) Dispatch in C++—Eli Bendersky

eli-bendersky.PNGA nice dive into performance costs on at least one compiler, and on the difficulties of doing meaningful performance measurements on modern hardware. Be sure to read the short comment thread too.

The Cost of Dynamic (Virtual Calls) vs. Static (CRTP) Dispatch in C++

by Eli Bendersky

From the article:

A couple of years ago I wrote an article about the Curiously Recurring Template Pattern in C++, focusing on the motivation behind it and how to implement it.

That article mentioned runtime performance as the main reason for employing CRTP instead of the more traditional runtime polymorphism (dispatch via virtual functions). While some rationale for the cost of virtual calls was given, I didn’t go too deep into it. Today I want to fix that by carefully analyzing the performance of virtual calls as opposed to the static calls made possible by CRTP.

Mandatory precaution about benchmarks

Benchmarking in 2013 is really hard. ...

Using STL Vectors, and Efficient Vectors of Vectors—Thomas Young

thomas-young.jpegThese are also the first two articles in a new blog by the creator of the PathEngine SDK.

Note: One or two of the points can be controversial, but the content is interesting and informative especially as an experience report.

Using STL Vectors

Efficient Vectors of Vectors

by Thomas Young

From the articles:

The stuff we do with vectors can be broadly categorised into two main use cases:

  • the construction of (potentially complex) preprocess objects, and
  • run-time buffers for queries, where buffer size requirements are fundamentally dynamic in nature

Preprocess objects include things like the pathfinding visibility graph. These objects can take time to build, but this is something that can be done by the game content pipeline, with preprocess data saved and loaded back from persistence by the game runtime.

The Curious Case of the Recurring Template Pattern—K-Ballo

Do you remember CRTP? No? Then definitely read about it here:

The Curious Case of the Recurring Template Pattern

by K-ballo

Inheritance is a handy tool to simplify design and avoi code duplication. However, good old fashioned inheritance is not always the right tool for the job. What if a base class could know, at compile time, who derives from it? Enter the curiously recurring template pattern (CRTP)...

Quick Q: A lambda’s return type can be deduced, so why can’t a function’s?—StackOverflow

Quick A: It can, in C++14. In fact, this C++14 feature supported today in current versions of GCC and Clang with -std-c++1y and in the Visual C++ November 2013 CTP.

On SO:

A lambda's return type can be deduced by the return value, so why can't a function's?

#include <iostream>

int main(){

    auto lambda = [] {
        return 7;

    std::cout << lambda() << '\n';


This program compiles and prints 7.
The return type of the lambda is deduced to the integer type based on the return value of 7.

Why isn't this possible with ordinary functions?

#include <iostream>

auto function(){
    return 42;

int main(){

    std::cout << function() << '\n';

error: ‘function’ function uses ‘auto’ type specifier without trailing return type