Articles & Books

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

Quick Q: Why have move semantics? -- StackOverflow

Quick A: Because value semantics are clean and naturally (exception-)safe, and avoid the brittleness of pre-C++11 workarounds that resort to owning raw pointers.

As often happens, the question contains the answer...

Why have move semantics?

... Can't the client already take advantage of pointers to do everything move semantics gives us? If so, then what is the purpose of move semantics?

Move semantics:

std::string f()
{
    std::string s("some long string");
    return s;
}
int main()
{
    // super-fast pointer swap!
    std::string a = f();
    return 0;
}

Pointers:

std::string *f()
{
    std::string *s = new std::string("some long string");
    return s;
}

int main()
{
    // still super-fast pointer swap!
    std::string *a = f();
    delete a;
    return 0;
}

Acquire and Release Fences Don't Work the Way You'd Expect -- Jeff Preshing

The C++11 standard makes a distinction between acquire and release fences and acquire and release operations. The differences are important and can affect correctness as well as performance.

[Ed.: These correctness subtleties are another reason to avoid standalone fences... in addition to the notes in this article, there are performance reasons to do so, as mentioned in Sutter's linked talk. std::atomics are the correct tool in nearly all cases where in the past you'd have reached for a standalone fence.]

Acquire and Release Fences Don't Work the Way You'd Expect

by Jeff Preshing

From the article:

... It's perhaps surprising, then, that this definition does not apply to standalone acquire and release fences in C++11! Those are a whole other ball of wax.

To see what I mean, consider the following two code listings. They’re both taken from my post about the double-checked locking pattern in C++11. The code on the left performs a release operation directly on m_instance, while the code on the right uses a release fence instead. ...

CppQuiz.org officially launched -- Anders Schau Knatten

Anders Schau Knatten recently launched the new site CppQuiz.org inspired largely by Olve Maudal's C++ pub quizzes. So grab a refreshing beverage, pull up a chair, and try a few to start off the first week of December...

CppQuiz.org officially launched!

by Anders Schau Knatten

From the announcement:

What is it

CppQuiz.org is (as you might have guessed by now) an online C++ quiz. Each question is a full C++ program, and you are to figure out what its output is. I stole this format from Olve Maudal's pub quizzes, but with one major difference: While his quizzes are about what happens on his computer (which is very interesting for a more interactive format), CppQuiz.org asks about what the standard mandates the output to be. If the example code doesn’t compile, or has unspecified/undefined behaviour, you answer that.

The site will just keep throwing questions at you (training mode), optionally giving you a hint and finally give you a full explanation of the answer, with references to the C++11 standard. If you want, you can however start a new quiz (quiz mode), and get a fixed number of questions. At the end you get a score, and a link to give your friends to see if they can beat you. Neither mode requires you to register or log in.

How you can help

If you like the quiz and want to help, there are many ways to do so:

  • Create your own questions, as many have done
  • Help improve the design (pull requests, e-mail, Twitter)
  • Help improve the functionality (pull requests, e-mail, Twitter)
  • Any other feedback (comments to this post, e-mail, Twitter)

Tips for founding new (C++) user groups -- Jens Weller

We're seeing quite a few new C++ user groups being formed, and so it's a good time to link to this timely article by Jens Weller:

Founding local C++ User Groups

From the article:

Lets start into the discussion on founding user groups, there is in my opinion different approaches to get started, but I don't want this to be a discussion, so I'll just list what I think is right. First I think that a C++ User Group should be local, which means its usually for a certain region. From my expierence, people are willing to travel up to 70km one way to a user group meeting. So in order to get started, I think you'll need the following four points:

  • People
  • Location
  • Topics
  • Date

...

Quick Q: When do you need to declare a variable constexpr? -- StackOverflow

Quick A: When you want to use the variable in a way that requires its value to be known at compile time.

Here's a short nugget that helps demonstrate the meaning of constexpr:

Why is constexpr required even though member function is constexpr?

The following does not compile unless I put constexpr before initializer_list:

constexpr std::initializer_list<int> il = {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
std::array<int, il.size()> a;

But initializer_list size is constexpr:

constexpr size_type size() const;