Articles & Books

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

Quick Q: Does access control matter for deleted constructors?

Quick A: No

Recently on SO:

Does access control matter for deleted constructors?

Since it's overload resolution that makes the program ill-formed in this case, and not access specifiers (which are checked later), there is no difference in outcome. The compiler will always complain that a deleted function was picked.

But since the idiom before C++11 was "declare but not define a private copy c'tor to disable copying", I would consider it going along with the same idiom, and therefore favorable. You are using the "old slang" with some new language to describe the same thing, except better.

East End Functions--Phil Nash

What's your opinion?

East End Functions

by Phil Nash

From the article:

There has been a recent stirring of attention, in the C++ community, for the practice of always placing the const modifier to the right of the thing it modifies. The practice has even been gifted a catchy name: East Const (which, I think, is what has stirred up the interest)...

Getting the Benefits of Strong Typing in C++ at a Fraction of the Cost--Vincent Zalzal

Very interesting!

Getting the Benefits of Strong Typing in C++ at a Fraction of the Cost

by Vincent Zalzal

From the article:

Strong types promote safer and more expressive code. I won’t repeat what Jonathan has presented already in his series on strong types.

I suspect some people may find that the NamedType class template has a nice interface but is using a somewhat heavy machinery to achieve the modest goal of strong typing. For those people, I have good news: you can achieve many of the functionalities of NamedType, with a very simple tool. That tool is the humble struct...