Designing a generic library

I consider the Standard Template Library an amazing piece of work that constitutes a milestone in the evolution of programming languages. Not only is it useful in itself, but it also brought into existence the notion of generic programming. What I find sometimes even more fascinating is the process of creating a generic library: of spotting patterns and making generalizations that while being useful on one hand, do not incur run-time cost on the other. If you are interested in this process, here is a couple of links that I recommend.

  1. Elements of Programming by Alexander Stepanov and Paul McJones. This book describes step-by-step the process of creating a generic library alternative to, but overlapping in scope wit the STL.
  2. A Concept Design for the STL. This work (by many authors) tries to identify concepts in the STL, define them formally, and use them to constrain the STL.
  3. Ranges for the Standard Library. This is an on-going effort driven by Eric Niebler to pin-point the concept of a range and use it in the STL. Something that is likely to become the future standard.
Posted in programming | Tagged | 1 Comment

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 , , , | 3 Comments

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 , , , , | 19 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