In this post we will see how C++ object lifetime can be used to control the duration of sessions: time spent owing and using a resource. The goal is to get a better understanding of what tools the language offers for using and sharing resources efficiently. Continue reading
“Concept” can mean two things in the context of C++ generic libraries:
- Something informal: something we know about template parameters, and can tell to other human programmers, e.g. in documentation.
- A language feature.
This post is about concepts in the first sense. It claims that we had concepts for quite a while already, and shows how we can use them to make generic libraries easier to use. Continue reading
In this post we will try to see by a practical example what Boost.Variant is for. You can sometimes see examples that use type
variant<int, double, string>, but to me they are artificial: I never needed to use something that is either a
int; but I still consider this library useful. Even if you are already familiar with Boost.Variant an its concepts of “never-empty guarantee” and “static visitor”, I made sure there is still something you can get from reading this post. Continue reading
In this post I want to describe a problem my colleagues have faced a couple of times recently, and show how it can be solved with C++. Here is the goal. We want to provide a function (or a set of overloaded functions) that would ‘do the right job’ for ‘practically any type’, or for ‘as many types as possible’. As an example of such ‘job’ consider
std::hash: what we want to avoid is the situation, where you want to use some type
X as a key in the standard hash-map, but you are refused because
std::hash does not ‘work’ for
X. In order to minimize the disappointment, the Standard Library makes sure
std::hash works with any reasonable built-in or standard-library type. For all the other types, that the Standard Library cannot know in advance, it offers a way to ‘customize’
std::hash so that they can be made to work with hash-maps.
For another popular example, consider Boost.Serialization library. Its goal is that almost any type should be serializable with the same interface: the library knows how to serialize, built-in,
boost popular types, and it offers a way to teach it to serialize new types.
We are going to see a number of ways how such a customizable framework can be implemented. Continue reading
This post is an introduction to another one that I intend to write in the future. The goal of this one is to go over the concepts of function template specialization, function (template) overloading, argument dependent lookup (ADL) and overload resolution. By no means do I intend to make it a complete reference. My only goal is to briefly show what these things are, what they can do, and what they cannot do, so that you can use them to solve practical problems. I tried to make this post as easy to understand as possible and to avoid the technical nomenclature where possible. Continue reading
The classes you design can do more (in terms of allowed operations) than what you could figure out from just looking at their member function declarations. The C++ Standard defines a number of cases where certain expressions involving your type are valid, even though there are no corresponding member function declarations. Sometimes this is just what you need; but sometimes the additional operations you never asked for can have grave negative impact on your program correctness. Continue reading