Articles & Books

2 Lines Of Code and 3 C++17 Features - The overload Pattern—Bartlomiej Filipek

Proof that you can do more!

2 Lines Of Code and 3 C++17 Features - The overload Pattern

by Bartlomiej Filipek

From the article:

While I was doing research for my book and blog posts about C++17 several times I stumbled upon this pattern for visitation of std::variant:

template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;

std::variant<int, float> intFloat { 0.0f };
    [](const int& i) { ... },
    [](const float& f) { ... },

With the above pattern, you can provide separate lambdas “in-place” for visitation.

It’s just two lines of compact C++ code, but it packs a few interesting concepts.

Let’s see how this thing works and go through the three new C++17 features that enable this one by one.

Why You Should Use std::for_each over Range-based For Loops—Jon Kalb

What do you think?

Why You Should Use std::for_each over Range-based For Loops

by Jon Kalb

From the article:

I’d like start by thanking Jonathan for creating and maintaining the Fluent{C++} blog, for the conversations that it spawns, and for letting me contribute with this guest post. Jonathan has invited me to add my thoughts on his previous posting, Is std::for_each obsolete?

Unit Testing C++ Templates and Mock Injection—Sumant Tambe

Do you unit test your C++ templates? It may be tricky as sometimes it's not clear how to inject mock code into a template. Traits can help! 

Unit Testing C++ Templates and Mock Injection Using Traits

by Sumant Tambe

About the Article:

Unit testing your template code comes up from time to time. (You test your templates, right?) Some templates are easy to test. No others. Sometimes it's not clear how to about injecting mock code into the template code that's under test. I've seen several reasons why code injection becomes challenging.

C++ Links #16—Bartlomiej Filipek and Wojciech Razik

The next episode of the ‘most useful C++ links’ is now available:

C++ Links #16

by Bartlomiej Filipek and Wojciech Razik

From the article:

See the most important and useful articles, podcasts and videos that happen between 19th and 25th of January 2019.

This week you will find a link to a ray-tracer, real-life examples of ranges, new useful features of Microsoft Visual Studio and many more!

Variant Visitation V2—Michael Park

Optimising accesses.

Variant Visitation V2

by Michael Park

From the article:

In 2015, I wrote an article titled Variant Visitation which described an implementation strategy for std::visit. The approach involved a matrix of function pointers, and many have raised concerns regarding poor code-gen caused by optimization limitations of function pointers on some compilers.

This post describes the switch-based approach implemented in mpark/variant, and its benchmark results...

C++ moves for people who don’t know or care what rvalues are—Topher Winward

Is it simpler that way?

C++ moves for people who don’t know or care what rvalues are

by Topher Winward

From the article:

When I was first learning about move semantics in C++, I kept reading articles that explained in terms of other scary sounding jargon — lvalues, rvalue references, memcpy, ownership. None of these things are strictly necessary to know about to understand the core of move semantics. (Though, the more you learn about them, the greater your understanding of move semantics will become.)

You may have heard of move semantics, and may know that they’re “faster”, but not why, or even how to move something. (Here “moves” and “move semantics” mean the same thing.)

This article will deliberately simplify or ignore some concepts (like constructors, rvalue references, stack vs heap) to make the core idea of moving easier to follow, so don’t worry if you already know this stuff and see something that isn’t technically correct. I’ll mark clarifications for these with a number. This article is aimed at those writing everyday (non-library) code, with little to no existing understanding of move semantics, to help get over the initial conceptual hurdle...

Stop reimplementing the virtual table and start using double dispatch

AndyG wants us to stop misusing dynamic_cast and start using a visitor pattern

Stop reimplementing the virtual table and start using double dispatch

by AndyG

From the article:

In this tutorial, I’ll talk about one solution to this problem I’ve had some success with: the double-dispatch Visitor pattern. With it, you can trim down those long if-else if blocks, separate responsibility into manageable pieces, and even stabilize your interface better.

Exploring C++20 - Class Types in Non-Type Template Parameters—Tobias Widlund


Exploring C++20 - Class Types in Non-Type Template Parameters

by Tobias Widlund

From the article:

If I had to pick out my favourite features planned for C++20, then this one would definitely be amongst the top 5 since I love compile time programming. This feature makes it more natural to write templated code since it allows you to group data together and pass it to a template without having to resort to hacks.

To explain what this feature is about, I will start by talking about normal non-type template parameters from pre-C++20...