intermediate

Introduction to std::chrono--Rachel Crawford

How to measure time in C++?

Introduction to std::chrono

by Rachel Crawford

From the article:

How many times have you tried to call a function that alleges to return a time value only to realise you don’t know what units the value is in? Or that takes a time value as a parameter, but doesn’t specify whether the value is expected to be in milliseconds, seconds, or hours?

Quick Q: Function not called in code gets called at runtime

Quick A: undefined behaviour can result in anything.

Recently on SO:

Function not called in code gets called at runtime

The program contains undefined behavior, as dereferencing a null pointer (i.e. calling foo() in main without assigning a valid address to it beforehand) is UB, therefore no requirements are imposed by the standard.

Executing never_called at runtime is a perfect valid situation when undefined behavior has been hit, it's as valid as just crashing (like when compiled with GCC). Okay, but why is Clang doing that? If you compile it with optimizations off, the program will no longer output "formatting hard disk drive", and will just crash...

C++ Weekly Episode 96: Transparent Lambda Comparators—Jason Turner

Episode 96 of C++ Weekly.

Transparent Lambda Comparators

by Jason Turner

About the show:

In this episode Jason explores the use of lambdas as comparators for the associative containers. Just how far can we take the use of lambdas? Variadic templates, forwarding references, multiple inheritance, variadic "using" declarations, local classes, transparent comparators and direct base class initialization are all utilized in this video.

A friendly type predicate--Andrzej Krzemieński

To improve the error messages.

A friendly type predicate

by Andrzej Krzemieński

From the article:

This is a sequel to the previous post on writing a custom type predicate. One of the readers on Reddit made a very insightful observation. The user has implemented a type that she intends to use with our library, call it Calc. She passes it to the library function, and she gets the compiler error:

static assertion failed: X does not have a desired interface

But what is our type missing? In the previous post we were describing 3 constraints. A concept could have far more of them. The user has already made an effort to have Calc comply with the constraints, so there must be something tiny missing. Maybe there is a bug in the implementation of the predicate? But it is difficult to track what it is that the predicate does not like about our type. We could use some more specific information...

Mixin Classes: The Yang of the CRTP--Jonathan Boccara

Some template tricks.

Mixin Classes: The Yang of the CRTP

by Jonathan Boccara

From the article:

Now that we’re clear on how the CRTP works, let me share with you another technique involving templates that is complementary to the CRTP: Mixin classes. I learnt about mixin classes by watching Arthur O’Dwyer’s Template Normal Programming talk at CppCon (actually you can find them in the slides because they were skipped over during the presentation)...

Quick Q: May a destructor be final?

Quick A: Yes, as any other virtual member function.

Recently on SO:

May a destructor be final?

May a C++ destructor be declared as final?

Yes.

And if so, does that prevent declaration of a derived class:

Yes, because the derived class would have to declare a destructor (either explicitly by you or implicitly by the compiler), and that destructor would be overriding a function declared final, which is ill-formed.

The rule is [class.virtual]/4:

If a virtual function f in some class B is marked with the virt-specifier final and in a class D derived from B a function D​::​f overrides B​::​f, the program is ill-formed.

It's the derivation itself that is ill-formed, it doesn't have to be used.

Is declaring a destructor to be final a workable idiom for indicating that a class is not intended to be used as a base class?

Effectively, but you should just mark the class final. It's quite a bit more explicit.

How to Use the STL With Legacy Output Collections--Jonathan Boccara

And how back_inserter works.

How to Use the STL With Legacy Output Collections

by Jonathan Boccara

From the article:

When you start using the STL and its algorithms in your code, it’s a bit of a change of habits. And then after a while you get used to it. Then it becomes a second nature. And then even your dreams become organized into beautifully structured ranges that fly in and out of well-oiled algorithms.

And when you reach that point, there is no coming back.

Until the day you come upon an old legacy structure that won’t let itself approached by the elegant and expressive way of coding that STL algorithms have. It’s a terrible encounter, where the beast tries to suck you back into the lengthy and dangerous quicksand of the raw for loops that now seemed so far away...

Simplifying Compile-Time Options With if constexpr--Philippe Groarke

New possibilities open in front of us!

Simplifying Compile-Time Options With if constexpr

by Philippe Groarke

From the article:

My latest little experiment relates to compile-time options and eliminating preprocessor checks in user code. I’m not a big fan of MACROs, especially when they are simply used to make compile-time branches. I am also not a fan of other techniques used to minimize this problem. With C++17, we now have a beautiful and simple tool that can help remove all these preprocessor checks, if constexpr...