Destructors — 2 use cases

In this post I want to describe an interesting observation: programmers generally use destructors for two purposes. One is fairly obvious: releasing resources; the other — not necessarily so. Continue reading

Posted in programming | Tagged , , , | 1 Comment

On operator<

Today I want to share with you a more philosophical issue I face with how the meaning of operator< is overloaded in C++. This could cause subtle bugs in programs. Continue reading

Posted in programming | Tagged , , , , | 14 Comments

Inadvertent conversions

Today I want to warn you about a bug in Boost.Rational library. This illustrates a certain danger connected with allowing convenience conversions from other types. Continue reading

Posted in programming | Tagged , , | 10 Comments

Inline functions

Inlining functions can improve or worsen your program’s performance (however you define ‘performance’). It has been described in detail in Herb Sutter’s GotW #33. Compiler can decide to inline your function, even if it was not declared inline and conversely: it can decide not to inline it even if it is declared inline. So, you might be tempted to think that declaring functions as inline has no useful portable meaning in C++. This is not so. I have found inline functions useful, and its usefulness has nothing to do with inlining. Continue reading

Posted in programming | Tagged | 13 Comments

Clever overloading

This post is intended to be a light introduction to certain C++ guru tricks. I want to introduce a couple of concepts that typically put normal people off. If you are an expert you will probably not learn anything new, except maybe for how to talk about such things to ordinary programmers. If you are an “ordinary programmer” with a happy family life, note that you really do not have to know these tricks to be a good, successful developer.

We are going to see a rather unusual problem connected to function overloading and explore the techniques of solving it. Continue reading

Posted in programming | Tagged , , , , | 17 Comments

Using variadic macros

Today I want to describe an issue I bumped onto recently. I had a function template that deals with two parameters — T and U and needed to make sure that one is convertible to the other. In C++11 I would write:

template <typename T, typename U>
T fun(T v, U u)
{
  static_assert(std::is_convertible<U, T>::value, "");
  // the code
}

But since I need my code that also needs to work on older compilers, I decided to go with Boost.StaticAssert. Continue reading

Posted in programming | Tagged , | 2 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. Continue reading

Posted in programming | Tagged , , | 2 Comments