Handling errors is canceling operations

I actually covered this topic before, in this post, but given my recent experience I feel it needs reiterating and a bit of restructuring. It boils down to the observation that any error handling I have encountered — be it error codes, errno, exceptions, error monad — is about canceling operations that depend, directly or indirectly, on the function that reported failure. This has some consequences on how we look at our program flow and what principles we should follow when responding to failures in our programs. Continue reading

Posted in programming | Tagged , , , | 6 Comments

(Not) using namespace std;

using namespace std;

This is called a using directive. Using it in code that is supposed to work for years is not a good idea.

Sometimes programmers are given advice to just type using namespace std; and all things from the Standard Library can be used easily, without bothering with namespaces. This is a good advice when you are learning C++ and when you are doing toy examples, or write small private programs. Because for these programs it is not a big deal if they cease to compile once you change the compiler or language version. Continue reading

Posted in programming | Tagged | 12 Comments

Deducing your intentions

The language feature in C++17 known as class template argument deduction was intended to supersede factory functions like make_pair, make_tuple, make_optional, as described in p0091r2. This goal has not been fully achieved and we may still need to stick to make_ functions. In this post we will briefly describe what class template argument deduction is, and why it works differently than what people often expect. Continue reading

Posted in programming | Tagged , , | 12 Comments

Treating symptoms instead of the cause

Compiler warnings can sometimes help us find bugs before we even build our program, but it only works provided that we can make use of the warnings. Especially, when we are aware that warnings detect only symptoms of the bugs rather than bugs themselves. Continue reading

Posted in programming | Tagged , , | 8 Comments

Faces of undefined behavior

I have been busy recently (doing C++-related stuff) and cannot find a spare time for preparing a decent blog post. I expect that to change in November. For the interim I am posting here my last year’s talk at code::dive: Faces of undefined behavior. It is relevant in the context of recent discussions about contracts in C++ and go-back-in-time optimizations.

Posted in programming | Tagged , , , , , | 1 Comment

Functions in std

Names of functions from the Standard Library used as pointers/references to functions can cause breakage in your code if you are upgrading to the newer version of C++ or compiling using a different implementation of the Standard Library, or even when just adding headers. In this post we will see why this is so, and what we can do about it.
Continue reading

Posted in programming | Tagged , | 9 Comments

Rvalues redefined

In this post we will explore what I consider the most significant language change in C++17. I call it the most significant because it changes the way you design your resource-managing types and how you think about initialization. It is often called “guaranteed copy elision”, but I will not use that name (except for this single time) because it does not reflect what this feature is. C++ has completely changed the meaning of rvalue (actually, prvalue). Continue reading

Posted in programming | Tagged , | 37 Comments

Testing C++ online

In this short post I wanted to share links to three online services that I use for testing different aspects of C++ and C++ compilers.

  • https://wandbox.org/ — this allows you to compile and observe the output of programs compiled in different versions of GCC and Clang, with different versions of Boost libraries;
  • https://godbolt.org/ — this allows you to see the assembly of C++ programs generated with different compilers on different platforms;
  • http://quick-bench.com/ — this allows you to micro-benchmark C++ functions on different versions of GCC and Clang using Google’s benchmark library.
Posted in programming | 3 Comments

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 the messages (also stored as std::strings) I receive. I cannot just type:

const std::string Special = "\0\0\0\0";

As this would be interpreted, according to C rules, as a zero-length string. But there is another constructor exactly for our case, where I can say “character C repeated N times”. So, I type:

const std::string Special ('\0', size_t(4));

I could have used braces for the initialization, but since I am aware of the std::initializer_list gotchas (like the one described here), I am following my rule of thumb, “if you suspect the type might expose an initializer-list constructor, and you want to initialize through a different constructor, do not use braces”.

Now I can test my program. It still doesn’t work correctly for some reasons. It does not recognize the special messages as special, even though I would expect it to. I test and debug it for quite some time, until finally do I realize that my string Special has a different value than I think. Continue reading

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