A = b = c, a strange consequence of operator associativity
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham Case study If you code in C++ regularly, you probably have encountered the following syntax: class Foo; Foo * make_Foo(); int main() { Foo * my_foo; if (my_foo = make_Foo()) { // ... Do things with the my_foo pointer } return 0; } In terms of semantics, this code is equivalent to the following: class Foo; Foo * make_Foo(); int main() { Foo * my_foo = make_Foo(); if (my_foo) { // ... Do things with the my_foo pointer } return 0; } This is today’s subject: assignment is an expres ..read more
Visit website
How to quantify technical debt inflation
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham If you work for a software company, you necessarily end up in a situation where you have a technical debt to repay, but don’t have the approval of your management to do so now. “We’ll deal with it later”, they say. But, as a good developer, you know two things: technical debt is harder to solve the more we wait to solve it, and dormant technical debt has a cost that is added to everything that is written in the meantime. When you try to argue “Technical debt is costly” to the said management, they answer “How much will it cost?”. But you ..read more
Visit website
Who owns the memory?
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham Have you ever heard of “ownership” of memory, in C++, when speaking about pointers? When you use raw pointers, you have to delete them (or else, you’ll leak memory). But if the said pointer is passed down functions and complex features, or returned by a factory, you have to know whose job it is to delete. Ownership means “responsibility to cleanup”. The owner of the memory is the one who has to delete its pointer. Deletion can either be explicit (through the keyword delete of the function free() regarding raw pointers) or bound to the lif ..read more
Visit website
3 interesting behaviors of C++ casts
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham This article is a little compilation1 of strange behaviors in C++, that would not make a long enough article on their own. Static casting an object into their own type can call the copy constructor When you use static_cast, by defaut (i.e. without optimizations activated) it calls the conversion constructor of the object you are trying to cast into (if it exists). For instance, in this code. class Foo; class Bar; int main() { Bar bar; static_cast<Foo>(bar); } The highlighted expression would call the following constructo ..read more
Visit website
Constant references are not always your friends
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham Early on, when we teach modern C++, we teach that every non-small1 data should be passed, by default, as constant reference: void my_function(const MyType & arg); This avoids the copy of these parameters in situations where they don’t need to be copied. Other situations call for other needs, but today we will focus on constant references. I found out that people tend to over-do const refs, thinking they are the best choice in every situation, and should be used everywhere they can be used. But are they always better than the altern ..read more
Visit website
I don’t know which container to use (and at this point I’m too afraid to ask)
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham As far as containers go in C++, since std::vector is well suited for most cases (with the occasional std::map when you need key-value association1), it’s become easy to forget that there are other types of containers. Each container has its strength and weaknesses, so if you are one to forget what its specificities are, this article is a good start. Disclaimer: Not all C++ containers are listed, only the most usable ones in my opinion. If you want to go further, you’ll find two very useful links in the addenda, at the bottom of the page ..read more
Visit website
Retrospective: The simplest error handler
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham This week, we’ll talk about last week’s article and try to be critical about it. There are a few things to say and it occurred to me (thanks to some feedback I got through social media) that it can be improved a lot. If you don’t know about SimplestErrorHandler, go read the article about it: One of the simplest error handlers ever written | Belay the C++ (belaycpp.com). The repo containing the feature is still available and is up-to-date with the changes I’ll present here: SenuaChloe/SimplestErrorHandler (github.com). Version 2: recursion ..read more
Visit website
One of the simplest error handlers ever written
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham This week, I’ll present you a small device I wrote to handle basic errors, the most compact and generic I could think of. It is certainly not perfect (mainly because perfection is subjective) but it is very light-weight and easy to use. If you want to skip the article and go directly to the source, here is the Github repo: SenuaChloe/SimplestErrorHandler (github.com) Specifications In terms of error handling, my needs are usually as follow: The error handler must write a message on the error output (std::cerr). The error handler must be ..read more
Visit website
The three types of development
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham This week we’ll discuss a serious topic affecting the developer community. This touches several languages, but the C++ community is one of the most affected by it1. There are several “ways” to write C++. I mean “way” as a collection of constraints and circumstances that will affect what you can do, what you should do, and how you can and should do it. This may seem vague, but think of it as types of environments that can drastically change your approach to the code you reading, editing, and writing. Based on my experience, I can distingui ..read more
Visit website
Prettier switch-cases
Belay the C++
by SenuaChloe
8M ago
Author: Chloé Lourseyre Editor: Peter Fordham I learned this syntax during a talk at the CppCon 2021 given by Herb Sutter, Extending and Simplifying C++: Thoughts on Pattern Matching using `is` and `as` – Herb Sutter – YouTube. You also find this talk on Sutter’s blog, Sutter’s Mill – Herb Sutter on software development. Context Say you have a switch-case bloc with no fallthrough (this is important), like this one: enum class Foo { Alpha, Beta, Gamma, }; int main() { std::string s; Foo f; // ... // Do things with s and f // ... switch (f ..read more
Visit website

Follow Belay the C++ on FeedSpot

Continue with Google
Continue with Apple
OR