Articles & Books

The promises and challenges of std::async task-based parallelism in C++11 -- Eli Bendersky

How to use std::async for task-based parallelism in C++11.

The promises and challenges of std::async task-based parallelism in C++11

by Eli Bendersky

From the article:

"Task-based parallelism" refers to a higher level of abstraction, where the programmer manages "tasks" - chunks of work that has to be done, while the library (or language) presents an API to launch these tasks. It is then the library's job to launch threads, make sure there are not too few or too many of them, make sure the work is reasonably load-balanced, and so on. For better or worse, this gives the programmer less low-level control over the system, but also higher-level, more convenient and safer APIs to work with. Some will claim that this also leads to better performance, though this really depends on the application.

One of C++ most underrated features: Namespace aliases--Jonathan Müller

A nice story with a goal:

One of C++ most underrated features: Namespace aliases

by Jonathan Müller

From the article:

About two months ago I wrote the following r/cpp comment:

[compte supprimé]

  • You could always just alias the namespace like namespace fnc = FunctionalPlus.

foonathan

  • Namespace aliases are one of C++ most underrated features.

In the thread a new library was presented. One user complained about the long namespace name, (s)he got the above replies. Judging by the number of upvotes, people seemed to agree with my comment. In this blog post I am going to elaborate it...

Searching and replacing in strings with boost

My series on building applications with Qt an boost continues:

Searching and replacing in strings with boost

by Jens Weller

From the article:

The next big milestone for my CMS is to actually generate HTML files, and I'm almost there. I'll reach it in the next two weeks, most code is written, just a little bit of refactoring is needed. This blog post is about searching and replacing in strings. As I started last week with implementing the functionality, that turns the data in my CMS into an HTML website.

There needs to be a lot of text transformed, in order to turn a shared structure like a cross page layout into a single, special HTML file, one of those transformations is, to replace the internal links with the correct links. A link to a different page in the same website cannot be represented as a text link, instead it is represented by a linkid, which corresponds to the Page it points to. This is to have still the correct link, if the page is renamed or moved...

Overload 131 is now available

ACCU’s Overload journal of February 2016 is out. It contains the following C++ related articles.

Overload 131

From the journal:

Defining Concepts
Concepts provide a new way of constraining code. Andrew Sutton shows us how to define and use them. by Andrew Sutton

On Zero-Side-Effect Interactive Programming, Actors, and FSMs
Functional programming is alien to many programmers. Sergey Ignatchenko considers parallels between actors and finite state machines.

Template Programming Compile Time Combinations & Sieves
Functional style frequently uses sequences. Nick Weatherhead applies these ideas to combinations in C++. by Nick Weatherhead

Classdesc: A Reflection System for C++11
C++ lacks direct support for reflection. Russell Standish brings an automated reflection system for C++, Classdesc, up to date. by Russell Standish

QM Bites : Maximising Discoverability of Virtual Methods
C++11 introduced override as a contextual keyword. Matthew Wilson encourages us to use it. by Matthew Wilson

So Why is Spock Such a Big Deal?
Spock testing in a Java environment is all the rage. Russel Winder talks through the history of testing on the JVM and demonstrates why Spock is so groovy. by Russel Winder

Modern C++ features: in-place construction--Arne Mertz

And emplace_back function:

Modern C++ features: in-place construction

by Arne Mertz

From the article:

Move constructors are often cheaper than copy constructors, which makes the construction and immediate relocation of objects in modern C++ more effective than in C++03. However, just moving the parts needed to construct the object in the right place can be even more effective. Several standard library functionalities use perfect forwarding to construct objects right where they are needed.

Sometimes you get things wrong--Marshall Clow

What is the best thing to return?

Sometimes you get things wrong

by Marshall Clow

From the article:

A few years ago, Sean Parent challenged me to provide an implementation of Boyer-Moore searching in C++. I did that, first in boost and then, later as part of the proposed Library Fundamentals Technical Specification.

The idea here is that you have a searcher object, which encapsulates the actual searching. You construct it with the pattern that you want to search for, and then you call the searchers operator() with the corpus that you want to search, and it will return to you the start of the pattern in the corpus, if it exists, and the end of the corpus, if it does not (this is the same set of rules that std::search follows).

But this weekend I realized that this is not the right thing to return. The searcher (and std::search for that matter) should return a “range” (ok, a pair of iterators) denoting the beginning and end of the pattern in the corpus. (Yes, you can get the end of the pattern by incrementing the returned iterator by the length of the pattern, but that’s an O(N) operation if you only have forward iterators...

Raw loops vs STL algorithms

A new post on the Meeting C++ blog, this time on <algorithm>

Raw loops vs. STL algorithms

by Jens Weller

From the article:

Since last week I am working on my CMS for static HTML pages again, and so the series about Building applications with Qt and boost continues. Today its about using STL algorithms, or how Sean Parent once said "no raw loops!". Now, I am not Sean Parent, and not event the implementers of the STL are perfect. Most code which I write is application code, which then powers Meeting C++. Also, I don't know all STL algorithms, and some times its just to tempting to write a little loop instead of searching the STL for the specific algorithm. Yesterday I had such a case.

Emulating C++17 Structured Bindings in C++14--John Bandela

How to use tuple return values with ease?

Emulating C++17 Structured Bindings in C++14

by John Bandela

From the article:

Bjarne Stroustrup back in Novemeber wrote a nice progress report, available here, of the Kona meeting. One of the proposals considered is called structured binding. The proposal addresses one of the inconveniences of returning multiple values from a function using tuples. While, it is very easy for a function to return multiple values, it is harder for the caller to use them. Here is an example from the write up.

consider the following function

tuple<T1,T2,T3> f() { /*...*/ return make_tuple(a,b,c); }

If we want to split the tuple into variables without specifying the type, we have to do this;

auto t = f();
auto x = get<1>(t);
auto y = get<2>(t);
auto z = get<3>(t);

The proposal puts forth the following syntax instead

auto {x,y,z} = f();               // x has type T1, y has type T2, z has type T3

I am excited for this feature, and for C++17 in general. While waiting for C++17, I decided to see how close I could get with C++14. Here is the result.

auto r = AUTO_TIE(x,y,z) = f();               // x has type T1, y has type T2, z has type T3

// Unlike the C++17 feature, you need to use r.x instead of just x
std::cout << r.x << "," << r.y << "," << r.z << "\n";