Articles & Books

break and fallthrough--Andrey Karpov

A classic error:

break and fallthrough

by Andrey Karpov

From the article:

We would like to suggest reading the series of articles dedicated to the recommendations on writing code of high quality using the examples of errors found in the Chromium project. This is the second part, which will be devoted to the switch operator and, more precisely, to the problem of a forgotten break operator...

Guidelines for constructor and cast design--Jonathan Müller

Interesting indeed!

Guidelines for constructor and cast design

by Jonathan Müller

From the article:

A while back — but sadly not too many blog posts ago — I wrote about explicit constructors and how to handle assignment. In this blog post, I made the assumption that you most likely want to have explicit single argument constructors.

But when do we actually want implicit single argument constructors?

Let’s consider the broader question: How should I design a cast operation for my user-defined type? And how should I design a constructor?

But first, something different: what is the difference between a cast and a constructor?

Do compilers take inline as a hint?--Simon Brand

Isn't that a good question?

Do compilers take inline as a hint?

by Simon Brand

From the article:

If you’ve spent any time in C or C++ communities online, you’ve probably seen someone say this:

inline used to be a hint for compilers to inline the definition, but no compilers actually take that into account any more.

You shouldn’t believe everything you see on the internet...

Producer-consumer with buffer swapping -- Krzysztof Ostrowski

Synchronisation cost minimisation technique explained based on the classic producer-consumer problem.

Lock less with swapped buffers

by Krzysztof Ostrowski

From the article:

Presented approach keeps the shared resource synchronised, but unblocks the producer execution for the time of the buffer items' consumption to achieve significant gain in overall performance of the solution and its reliability.

Abseil's String Utility APIs -- Tom Manshreck

Abseil's C++ Tips of the Week for January 26th

Abseil's String Utility APIs

by Tom Manshreck, Abseil Tech Writer

About the article:

Abseil provides many important string utilities such as StrCat(), StrSplit() and StrJoin(). Read the tips that introduced these APIs at Google and explain how to use them now at https://abseil.io/tips/3  https://abseil.io/tips/10 , https://abseil.io/tips/36  and https://abseil.io/tips/59.

C++17: Initializers for if & switch statements--Marc Gregoire

Small reminder:

C++17: Initializers for if & switch statements

by Marc Gregoire

From the article:

Two small, but very useful C++17 features are initializers for if and switch statements. These can be used to prevent polluting the enclosing scope with variables that should only be scoped to the if and switch statement. The for statement already supports such initializers since the beginning...

Quick Q: Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?

Quick A: This is not a valid statement.

Recently on SO:

Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?

The statement (4 > y > 1) is parsed as this:

((4 > y) > 1)

The comparison operators < and > evaluate left-to-right.

The 4 > y returns either 0 or 1 depending on if it's true or not.

Then the result is compared to 1.

In this case, since 0 or 1 is never more than 1, the whole statement will always return false.

Pimpl vs Abstract Interface - a practical tutorial -- Bartlomiej Filipek

In the article we describe how to limit compilation dependencies. Two methods are discussed: pimpl approach and via abstract interfaces

Pimpl vs Abstract Interface - a practical tutorial

by Bartlomiej Filipek

From the article:

In the post I covered the pimpl pattern. I discussed the basic structure, extensions, pros and cons and alternatives. Still, the post might sound a bit “theoretical”. Today I’d like to describe a practical usage of the pattern. Rather than inventing artificial names like MyClass and MyClassImpl you’ll see something more realistic: like FileCompressor or ICompressionMethod.

How a weak_ptr might prevent full memory cleanup of managed object -- Bartlomiej Filipek

Tricky weak_ptr and shared_ptr interaction:<img alt="" data-cke-saved-src="https://2.bp.blogspot.com/-2XIQxeCUBFc/Wi2JGz_EcXI/AAAAAAAADNU/vEkN6ZtXR-Yon5YuZWBxk-6kjmHD1011ACLcBGAs/s1600/control_block.png" src="https://2.bp.blogspot.com/-2XIQxeCUBFc/Wi2JGz_EcXI/AAAAAAAADNU/vEkN6ZtXR-Yon5YuZWBxk-6kjmHD1011ACLcBGAs/s1600/control_block.png" right;"="" style="float: right; width: 230px; height: 199px;">

How a weak_ptr might prevent full memory cleanup of managed object

by Bartlomiej Filipek

From the article:

It appears that in some cases memory allocated for the object controlled by smart_ptr might not be released until all weak pointers are also ‘dead’.