intermediate

Lightning Talks from Meeting C++ 2019 are now online!

The lightning talks from Meeting C++ 2019 are now online!

Meeting C++ Youtube Channel

by Jens Weller

From the article:

A few lightning talks I'd like to point to:

Finding hard to find bugs with Address Sanitizer - Marshall Clow

Consistently Inconsistent - Conor Hoekstra

Why don't the cool kids like OOP? - Jon Kalb

How to initialize x from expression y - Howard Hinnant

How to Merge Consecutive Elements in a C++ Collection--Jonathan Boccara

Simple and sweet.

How to Merge Consecutive Elements in a C++ Collection

by Jonathan Boccara

From the article:

Merging identical consecutive elements in a collection is a recurring need, in C++ or elsewhere in programming.

For example, we could want to aggregate a collection of hourly results into a collection of daily results: all the results of each day get aggregated into one for that day. In this case, being “identical” means being on the same day, and “aggregating” means taking two results with a common date, and creating a result at this date and with the sum of their amounts...

Quick Q: When to use virtual destructors?

Quick A: When you might delete polymorphically.

Recently on SO:

When to use virtual destructors?

Virtual destructors are useful when you might potentially delete an instance of a derived class through a pointer to base class:

class Base
{
    // some virtual methods
};

class Derived : public Base
{
    ~Derived()
    {
        // Do some important cleanup
    }
};

Here, you'll notice that I didn't declare Base's destructor to be virtual. Now, let's have a look at the following snippet:

Base *b = new Derived();
// use b
delete b; // Here's the problem!

Since Base's destructor is not virtual and b is a Base* pointing to a Derived object, delete b has undefined behaviour...

What Every C++ Developer Should Know to (Correctly) Define Global Constants--Jonathan Boccara

C++17 to the rescue.

What Every C++ Developer Should Know to (Correctly) Define Global Constants

by Jonathan Boccara

From the article:

Constant values are an everyday tool to make code more expressive, by putting names over values.

For example, instead of writing 10 you can write MaxNbDisplayedLines to clarify your intentions in code, with MaxNbDisplayedLines being a constant defined as being equal to 10.

Even though defining constants is such a basic tool to write clear code, their definition in C++ can be tricky and lead to surprising (and even, undefined) behaviour, in particular when making a constant accessible to several files.

Everything in this article also applies to global variables as well as global constants, but global variables are a bad practice contrary to global constants, and we should avoid using them in the first place...