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 | 9 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


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

Dangerous int-to-string conversions

Converting between a string and an int is a problem without a satisfactory solution in C++. Solutions range from itoa, strtol, through std::stringstream to boost::lexical_cast, but every one has its downsides. You have probably already figured out what to do yourself, but have you ever seen this:

bool convert(std::string& to, const int& from)
  to = from;
  return true;

Is this even legal C++? Yes, it compiles fine! Do you know what it does? Continue reading

Posted in programming | Tagged , | 17 Comments

noexcept — what for?

In this post I would like to share my observation on where using noexcept really adds value. It is less often than what one might expect, and it does not have that much to do with throwing or not throwing exceptions. The conclusion surprises me a bit, and I hesitate to present it because it is counter to the advice I hear from people I consider authorities on the subject. Continue reading

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

Common optimizations

Language designers, compiler and library vendors make a great effort to make your programs run faster and faster. This post is a tour of some common performance optimizations in C++.

Consider the following code that deals with std::string:

string makeText()
  string s{"Hello World!"}; // C++11 init syntax
  return s;

int main()
  string t = makeText();
  cout << t << endl;

std::string is a specialization of template std::basic_string which takes an allocator as one of template arguments. This allocator is used — unsurprisingly — for allocating memory. Memory allocation is a fairly slow operation that is best avoided. The question is: how many times function allocate (on the allocator) will be invoked for the purpose of memory allocation during this program’s execution? 3, 2, 1, 0? Continue reading

Posted in programming | Tagged , , , | 26 Comments