Tag Archives: c++14

String’s competing constructors

Let’s start with the problem. I want to check whether a program received a text message that consists of four consecutive zeroes. Not ‘0’, but the numeric zero. I will create a constant std::string representing the special sequence and compare … Continue reading

Posted in programming | Tagged , , , , | 20 Comments

Your own type predicate

In this post we will see how to define a type trait or a type predicate or a meta-function that would allow us to check at compile time whether a type exposes an interface that we need. That is, we … Continue reading

Posted in programming | Tagged , , , , , , | 19 Comments

Compile-time string concatenation

We will start with a bug, taken from real life. It spans across three files: Question: what happens when this program is executed?

Posted in programming | Tagged , , , , , | 35 Comments

Asserts in constexpr functions

Assertions (like C-style macro assert) are not an ideal, but still useful tool for indicating assumptions about program correctness, and help finding programmer bugs. In this post we will see how we can use assertions in constexpr functions. This works … Continue reading

Posted in programming | Tagged , , , , | 20 Comments

A conditional copy constructor

In this post we will try to define a ‘wrapper’ class template that does or does not have a copy constructor depending on whether the wrapped class has it. This will be a good opportunity to explore in depth a … Continue reading

Posted in programming | Tagged , , , , , | 6 Comments

Ref-qualifiers

In this post I want to describe a small language feature added in C++11 that, although essential for full value semantics support, is often neglected in tutorials and in compiler implementations.

Posted in programming | Tagged , , | 6 Comments

Find the bug

Today, let’s take a short test. Find what is likely to be a bug in the following code and suggest how to fix it. You may object to thus stated task. We only see a small portion of the code. … Continue reading

Posted in programming | Tagged , | 38 Comments

Too perfect forwarding

Update. One of the readers informed me that the same problem has been already described by R. Martinho Fernandes (see here), Eric Niebler (see here) and Scott Meyers (see here); so I should at least link to their articles. Now … Continue reading

Posted in programming | Tagged , , , | 25 Comments

“constexpr” function is not “const”

This is just a word of caution. C++14 will not be backwards compatible with C++11 in one aspect of constexpr functions. In C++11, when you define a constexpr member function, it implicitly obtains a const qualifier: The first declaration of … Continue reading

Posted in programming | Tagged , | 9 Comments