Articles & Books

compile-time iteration with C++20 lambdas—Vittorio Romeo

This article covers various compile-time "iteration" constructs that rely on the upcoming "familiar template syntax for lambdas" C++20 feature.

compile-time iteration with C++20 lambdas

by Vittorio Romeo

From the article:

In this article I'm going to show you how to implement the above constructs, relying on a new nifty addition to C++20 lambdas: [P0428: "Familiar template syntax for generic lambdas"], by Louis Dionne. [...]

It shows how to implement constructs for the following operations:

Iterating over a list of types;
Iterating over a list of compile-time values;
Iterating over a compile-time integral range;
Enumerating a list of types alongside their indices.
The code provided works on g++ 8

How to Pass a Polymorphic Object to an STL Algorithm—Jonathan Boccara

Did you ever try?

How to Pass a Polymorphic Object to an STL Algorithm

by Jonathan Boccara

From the article:

As we can read in the opening chapter of Effective C++, C++ is a federation of 4 languages:

  • the procedural part coming from C,
  • the object-oriented part,
  • the STL part (following a functional programming paradigm),
  • the generic part with templates.

And what’s more, all of those 4 sub-languages are part of one whole: the C++ language. Those 4 paradigms begin united in one language gives opportunities for them to interact – and often, those interactions create interesting situations.

Today we’re focusing on one particular interaction, between the object-oriented model and the STL. There could be multiple forms for this interaction, and the case we will look at is how to pass a polymorphic (that is, having virtual methods) function object to an STL algorithm.

uninitialized_tag in C++—Marius Elvert

Optimise or not?

uninitialized_tag in C++

by Marius Elvert

From the article:

No doubt, C++ is one of those languages you can use to squeeze out every last drop of your CPU’s processing power. On the other hand, it also allows a high amount of abstraction. However, micro-optimization seldom works well with nice abstractions...

C++17: The two line visitor explained—Marius Elvert

The power of C++17.

C++17: The two line visitor explained

by Marius Elvert

From the article:

If you have ever used an “idiomatic” C++ variant datatype like Boost.Variant or the new C++17 std::variant, you probably wished you could assemble a visitor to dispatch on the type by assembling a couple of lambda expressions like this:

auto my_visitor = visitor{
  [&](int value) { /* ... */ },
  [&](std::string const& value) { /* ... */ },
};

Quick Q: With “-fno-exceptions”, what happens with “new T”?

Quick A: The behaviour will likely stay the same.

Recently on SO:

With “-fno-exceptions”, what happens with “new T”?

The way I understand it, operator new is defined by libstdc++. If you now compile your own code with -fno-exceptions, you cannot catch any exceptions, but you will still be linking against the normal version of libstdc++, which does throw an exception.

So yes, new T will throw an exception, even with -fno-exception.

However, if you compiled libstdc++ with -fno-exception as well, things become different. Now, new T cannot throw an exception but, if I read the libstdc++ manual right it will call abort() instead.

It seems that, if you want your new T to return NULL on failure, the only way is to explicitely specify nothrow...

Which One Is Better: Map of Vectors, or Multimap?—Jonathan Boccara

Depends!

Which One Is Better: Map of Vectors, or Multimap?

by Jonathan Boccara

From the article:

While advising on how to make code more expressive on the SFME project, I came across an interesting case of choosing the right data structure, which I’ll share with you with the permission of the authors of the projects.

We had to associate a key with several values, and perform various operations. Should we use a map of vectors, or is a multimap more appropriate? Let’s see the case in more details, and compare the two solutions...

Overload 144 is now available

ACCU’s Overload journal of April 2018 is out. It contains the following C++ related articles.

Overload 144 is now available

From the journal:

Deeds not words
Women’s suffrage used the motto “Deeds not Words". Frances Buontempo applies this to programming. by Frances Buontempo

No News is Good News
Using ‘new’ without care can be slow. Paul Floyd uses Godbolt’s compiler explorer to see what happens when you do. by Paul Floyd

Monitoring: Turning Noise into Signal
Creating useful logging is a constant challenge. Chris Oldwood shows us how structured logging helps. by Chris Oldwood

The Interface to Component Pattern and DynaMix
Dynamic Polymorphism is hard in C++. Borislav Stanimirov demonstrates how the DynaMix library helps. by Borislav Stanimirov

5 Reasons NOT to Use std::ostream for Human-Readable Output
C++’s ostream can be hard to use. Sergey Ignatchenko suggests we use the {fmt} library instead. by Sergey Ignatchenko

Practical Cryptographical Theory for Programmers
Cryptography is a daunting subject. Deák Ferenc helps you get started. by Deák Ferenc

Ex Hackina
Machine Learning and AI are popular at the moment. Teedy Deigh takes the Turing test. by Teedy Deigh