I like to std::move it

A little while ago while I was surfing the web I stumbled on a blog of a C++ programmer. Sadly, I was unable to retrace my steps, so I will have to reconstruct what I found there from memory. The programmer was faced with the task of multiplying two matrices with each other. The result is yet another matrix. Efficiency and ease of use were important criteria. Here is the relevant part of the code:

matrix & matrix::operator* (matrix const & other) const
{
	// snip: ... assert matrix sizes are compatible ...
	matrix * result = new matrix(rows(), other.columns());
	// snip: ... compute and store matrix elements ...
	return *result;
}

Let us put on our CSI C++ hat for a while.

Continue reading “I like to std::move it”

May the FORs be with you

Loops are among the basic constructs you find in nearly all programming languages. Of the available variations, for loops are the most common ones. The word for is so small that it is not affected by the tax on long keywords and variable names that programming languages seem to be subject of. Thus, you will find for as a keyword in most of them.

C++11 supports a multitude of styles to write for loops. Let us have a look at some of these possibilities for a simple example.

Continue reading “May the FORs be with you”

Mopping up template barf with static_assert

Templates are a mixed blessing for C++ developers. On the one hand, templates avoid code duplication as most impressively demonstrated by the standard template library. The template mechanism, in contrast to preprocessor macros, is aware of the C++ language and part of it. Compilers can produce helpful error messages – and some compilers even do so.

Some compilers, however, fail in providing clear template compilation error messages. If you make a mistake, you are greeted by a bucket load of irritating notes on what went wrong in detail. Since it is down to you to identify the relevant chunks, this is often referred to as template barf.

Continue reading “Mopping up template barf with static_assert”