Today’s blog post is about something that should be simple and apparently it causes trouble: how to declare a qHash overload for a custom datatype. This is necessary when we want to use custom datatypes as keys in a QHash. From the documentation: A QHash’s key type has additional requirements other than being an assignable […]
Blog Archives
VS Code for Qt Applications – Part 2 A Technical Guide
This 3-part blog series shows in detail how to get up to speed with a Qt project using Visual Studio Code. Part 2 shows how to get a complete setup for qmake and CMake projects, with a deeper look at the Qt side.
Qt, range-based for loops and structured bindings Part 1
Qt has a long history. The first stable version was released before the first version of C++ was standardized and long before the different C++ compiler vendors started shipping usable implementations of the C++ standard library. Because of this, Qt often followed (and still follows) some design idioms that feel unnatural to the usual C++ […]
Getting rid of “volatile” in (some of) Qt
The upcoming version of the C++ Standard (C++2a) is proposing to deprecate certain usages of the volatile keyword, by adopting the P1152 proposal (Deprecating volatile). Despite the somewhat “flamboyant” title, the actual deprecated parts are very limited and indeed the paper limits the deprecation to somehow language/library corner cases and/or dangerous antipatterns. For instance certain […]
How to cast a function pointer to a void* a slightly-too-long-for-twitter report about a subtlety in C++
Question: How do you cast a function pointer to a void*? (You’re trustworthy and wouldn’t dare to do anything evil with it, right?) Answer: Maybe your first thought was, well reinterpret_cast<void*>(&f) of course. That was mine, at least. Well, it turns out, C++ wants to support architectures, where data and instruction pointers are physically distinct […]
C++ Modernization Brochure
New releases of the C++ language maintain incredibly strong backwards compatibility, making it easy to keep older C++ code working properly as standards march forward. C++11, C++14, and C++17 have transformed the C++ language in ways that make it as programmer-friendly as more recent languages but with many essential benefits that continue to make it […]
Un-deprecate your Qt project
The last post from my colleague Marc Mutz about deprecating Q_FOREACH caused quite an uproar amongst the Qt developers who follow this blog. I personally feel that this was caused fundamentally by a perceived threat: there is a cost associated to porting away a codebase from a well-known construct (Q_FOREACH) to a new and yet-undiscovered […]
Stepanov-Regularity and Partially-Formed Objects vs. C++ Value Types
In this article, I will take a look at one of the fundamental concepts introduced in Alex Stepanov and Paul McJones’ seminal book “Elements of Programming” (EoP for short) — that of a (Semi-)Regular Type and Partially-Formed State. Using these, I shall try to derive rules for C++ implementations of what are commonly called “value […]
Tuple And Pair in C++ APIs? A Simple Design Goal to Improve Your C++ APIs
Quick: When you design C++ APIs, when and how should you use pair and tuple? The answer is as simple as it is surprising: Never. Ever. When we design APIs, we naturally strive for qualities such as readability, ease-of-use, and discoverability. Some C++ types are enablers in this regard: std::optional, std::variant, std::string_view/gsl::string_span, and, of course, […]
Four Habit-Forming Tips to Faster C++
Are you a victim of premature pessimisation? Here’s a short definition from Herb Sutter: Premature pessimization is when you write code that is slower than it needs to be, usually by asking for unnecessary extra work, when equivalently complex code would be faster and should just naturally flow out of your fingers. Despite how amazing today’s […]