Kategori: C++

  • Guidelines for commenting code

    Writing good comments is hard and we developers have come up with several excuses to not write comments at all. In this post I will try to debunk some of the most common excuses for not writing comments and give some good reasons for writing them and how to write them so that they add […]

  • Spaceship simplifications

    I ended the last post by stating that I would provide an example of how you can implement the new C++ spaceship operator for your own class. In this post I will show you how that can be done and how it can simplify your implementation a lot. Assume that you have defined your own […]

  • C++ Spaceship operator usage

    C++20 introduced a new three-way comparison operator, also known as the spaceship operator. Here I explain how it works and show some simple examples. First, let’s take a look at some code, which I soon will explain: This code shows a simple three-way comparison of two integers. Since comparing the integers can have three different […]

  • Get to know constexpr

    I did my first run with C++ before modern C++ was a thing. That is, before C++11 and C++14. Now that I’m once again developing application code using C++ I’ve learned to master great things like smart pointers, move semantics, and lambda expressions. One modern C++ concept that I haven’t put so much thought to […]

  • When an int is not an int

    The more I learn about C++ the more I understand that it is considered a difficult language to master. It has all these quirks and things that are far from obvious which may lead to unforeseen behavior. As an example, look at the following code: From the headline of this post and the introductory text […]

  • Hidden conditions

    One way of making code easy to understand and reason about is to try to ensure that it does exactly what you expect it to do. Sometimes however, there are conditions secretly hidden in places of the code that leaves you scratching your head and forces you into a quest for answers down the deepest […]

  • The art of keeping things simple

    As a software developer you go through different phases in your personal development. Many, but far from all, end up valuing simple code. Simple does not imply that the code doesn’t do what it needs to do, it means that the code is written in a way that puts minimal cognitive load on the human […]

  • I found a case for a ”C/C++ developer”

    What are your thoughts when you see a job ad where they are looking for a C/C++ developer? Personally I have always wondered what ”C/C++” means. Do they know that C and C++ are different languages? That C is a procedural language and C++ is an object oriented language and writing modern C++ code is […]

  • Ways to pass Arguments in C++

    In C++ there are many ways to pass an argument to a function. The following code shows some commonly used variants. It is also possible to add the const keyword at different places when passing by pointers to make either the pointer const or the value the pointer is pointing at const, or both. You […]

  • Representing missing value in C++

    One thing that I have bothered me quite a lot when coding in C# is a good way to indicate the absence of a value. There are a few different alternatives how this can be done, the most common being using null. However, using null instead of an actual type quite often leads to crashes […]