Articles & Books

Tippet: Use reference_wrapper to create views of data -- Indi

Explicit C++ describes how to use std::reference_wrapper to create alternative views of data.

Tippet: Use reference_wrapper to create views of data

by Indi

from the article:

When working with objects indirectly, always use references. Only use pointers to indicate optional referencing. But there’s one little hitch: because you can’t rebind references, you can’t simply have a container of references. Enter std::reference_wrapper.

The C++ highlights and more of GCC 5.1

The release of GCC 5.1 is a highlight, here is an overview on the most important new things:

The C++ highlights of GCC 5.1 and more

by Jens Weller

From the article:

Just recently, GCC 5.0 has been released as GCC5.1, the not only the newest version of GCC, but also bumping up the version number from 4 to 5. This release is a major milestone for GCC, but also for C++, as it brings full C++14 support, but yet not C++11(std=c++11) as the new default...

Coupling and Cohesion -- Paul Watt

coupling-lego.PNGA solid and entertaining treatment with good C++ examples:

Coupling and Cohesion

by Paul Watt

From the middle of the article:

... To further clarify this statement, not all code should be reused.

Why Not?

Because the majority of software that is written is designed to fulfill a very specific purpose. The challenge is to find the balance point between generic reusable building blocks, and a tool or application that meets a need. Generic software components are very valuable, however, they are almost useless by themselves...

And gems like this:

... For every statement of code that you write or use, you should ask the question: Does this add value or risk?

Don't blame initializer_list prematurely -- Marco Arena

A post on three common pitfalls regarding initializer_list...or not? 

Don't blame initializer_list prematurely

by Marco Arena

From the article:

“Cannot convert initializer list argument to ‘int*'”. People started trying to figure out why initializer_list was not covertible to int[]. [...] A gentleman spotted the following in the dark corners of the codebase:

vector<YahtzeeGame> games;
games.push_back(make_tuple(5, 6, 2));
games.push_back(make_tuple(5, 6, 3));
games.push_back(make_tuple(5, 6, 4));
// other stuff

Excited about C++11, he tried to refactor:

vector<YahtzeeGame> games = { {5, 6, 2}, {5, 6, 3}, {5, 6, 4} };
And does it compile? ...

Regular expression in c++11/14 -- GuoJiufu

Overview on Regular Expression Support in Modern C++.

   Regular expression in c++11/14

   by GuoJiufu

From the article:

Regular expression is a useful string process utility for handling both pure text and markup language text like html. There are a few useful tools that are designed to use regular expression, such as  grep and sed. In development language, most script languages, for example, javascript or perl, support regular expression. Regular expression has also been introduced into java as module for a long time. In C, the most famous regular expression library among many of those libraries developed would be POSIX regular expression API. As for C++, boost regular expression library was developed a few years ago. Then in C++11, regular expression was introduced into the standard library as <regex>.  This blog is going to explain what is regular expression and how it is supported in c++11/14.

boost 1.58 - a short overview -- Jens Weller

Short overview on the new libraries introduced in boost 1.58.

   boost 1.58 - a short overview

   by Jens Weller

From the article:

A new version of boost has been released, version 1.58 brings two new libraries and a lot of bug fixes (fixes for 28 libraries are listed). Also, a good resource for boost is the new and updated book about the boost libraries from Boris Schäling, which is also available online. Not included in this release is boost.compute, a library earlier this year accepted into boost and presented at C++Now, maybe it will be in 1.59 later this year.

Making Boost.Signals2 More OOP-Friendly -- Pavel Frolov

This article shows how modern C++ features, notably variadic templates and perfect forwarding, can be used to implement a generic variant of observer pattern without the help of either macros or proprietary compiler extensions.

Making Boost.Signals2 More OOP-Friendly

by Pavel Frolov

From the article:

The observer design pattern is by far the most popular and widely known among behavioural patterns. Unfortunately, unlike other mainstream languages out there, the C++ standard library doesn’t provide out of the box observer implementation. Luckily, Boost contains Signals2, a signal/slot library which can serve as a basis for an observer. Using Signals2 as it is, however, is not so convenient in object-oriented program due to the need of manually coded register and notify class methods for each of signal/slot pairs. This article suggests an observable mixin which attempts to solve the outlined problem.

Quick Q: Can I do something like range-for with a counting loop?

Quick A: Yes. Start with boost::irange.

Recently on SO:

Can I do something like range-for with a counting loop?

Actually this are two related questions.

I know there is new syntax in C++11 for range based for loops of the form:

//v is some container
for(auto &i: v){
   // do something with i
}

First question: how can I infer at which iteration I am in this loop? (Say I want to fill a vector with value j at position j).

Second question: I wanted to now if there also is some other way to write a loop of the form

for(int i=0; i<100; i++){ ... }

I find this way of writing it a bit cumbersome and I do this so often and I would love to have a more concise syntax for it. Something along the lines:

for(i in [0..99]){ ... }

would be great.

For both questions I would like to avoid having to use additional libraries.

Quick Q: How is “=default” different from “{}” for default constructor and destructor?

Quick A: The "= default" keeps the type trivial.

Recently on SO:

How is “=default” different from “{}” for default constructor and destructor?

I originally posted this as a question only about destructors, but now I'm adding consideration of the default constructor. Here's the original question:

If I want to give my class a destructor that is virtual, but is otherwise the same as what the compiler would generate, I can use =default:

class Widget {
public:
   virtual ~Widget() = default;
};

But it seems that I can get the same effect with less typing using an empty definition:

class Widget {
public:
   virtual ~Widget() {}
};

Is there any way in which these two definitions behave differently?

Based on the replies posted for this question, the situation for the default constructor seems similar. Given that there is almost no difference in meaning between "=default" and "{}" for destructors, is there similarly almost no difference in meaning between these options for default constructors? That is, assuming I want to create a type where the objects of that type will be both created and destroyed, why would I want to say

Widget() = default;

instead of

Widget() {}

?

I apologize if extending this question after its original posting is violating some SO rules. Posting an almost-identical question for default constructors struck me as the less desirable option.

Boost.Spirit + Flatbuffers + Catch + Biicode + CLion + Github--Max Galkin

Come see a nice product review:

Boost.Spirit + Flatbuffers + Catch + Biicode + CLion + Github

by Max Galkin

From the article:

This weekend I’m test-driving several new technologies. OK, OK, maybe not so new for everyone else, but new for me. I’m trying to write a parser for the Flatbuffers IDL grammar using Boost.Spirit, I’m referencing Boost using Biicode, I’m testing the code using Catch, I’m editing the project in CLion, and I’m pushing the results to Github yacoder/flatbuffers-with-spirit!