Articles & Books

Better code understanding with Sourcetrail -- Bartlomiej Filipek

Let's have a look at Sourcetrail, a great tool for code (C++/Java) visualtiozation:

Better code understanding with Sourcetrail

by Bartlomiej Filipek

From the article:

I’m exploring the tool, and overall I am impressed! It works very well; the setup is easy to do, there’s a lot of help, beautiful and dynamic diagrams (even with smooth animations), under active development… what else would I want? smile

The C++ Bestiary -- Adi Shavit

Need some last minute Halloween costume inspiration?

The C++ Bestiary ��

by Adi Shavit

From the article:

C++ is blessed with a plethora of gotchas, traps, caveats, pitfalls and footguns. Within the C++ dungeons lurk many shady characters. ‘Tis the time of year to meet some of these bountifully spawned beasts.

How not_null can improve your code? -- Bartlomiej Filipek

Let’s investigate what "not_null" (from the Core Guidelines/Guideline Support Library) can do for us.

How not_null can improve your code?

by Bartlomiej Filipek

From the article:

I believe "not_null" can help in many places. It won’t do the magic on its own, but at least it forces us to rethink the design. Functions might become smaller (as they won’t have to check for nulls), but on the other hand, the caller might require being updated.

Quick Q: can i use move only exception throwable objects with vectors?

Quick A: Yes, but with unspecified behavior in case of exception thrown.

Recently on SO:

Using an object without copy and without a noexcept move constructor in a vector. What actually breaks and how can I confirm it?

A vector reallocation attempts to offer an exception guarantee, i.e. an attempt to preserve the original state if an exception is thrown during the reallocation operation. There are three scenarios:

  1. The element type is nothrow_move_constructible: Reallocation can move elements which won't cause an exception. This is the efficient case.
  2. The element type is CopyInsertable: if the type fails to be nothrow_move_constructible, this is sufficient to provide the strong guarantee, though copies are made during reallocation. This was the old C++03 default behaviour and is the less efficient fall-back.
  3. The element type is neither CopyInsertable nor nothrow_move_constructible. As long as it is still move-constructible, like in your example, vector reallocation is possible, but does not provide any exception guarantees (e.g. you might lose elements if a move construction throws).

The normative wording that says this is spread out across the various reallocating functions. For example, [vector.modifiers]/push_back says:

If an exception is thrown while inserting a single element at the end and T is CopyInsertable or is_nothrow_move_constructible_v<T> is true, there are no effects. Otherwise, if an exception is thrown by the move constructor of a non-CopyInsertable T, the effects are unspecified.

I don't know what the authors of the posts you cite had in mind, though I can imagine that they are implicitly assuming that you want the strong exception guarantee, and so they'd like to steer you into cases (1) or (2).

Mutable--Arne Mertz

Do you know that keyword?

Mutable

by Arne Mertz

From the article:

The mutable keyword seems to be one of the less known corners of C++. Yet it can be very useful, or even unavoidable if you want to write const-correct code or lambdas that change their state...

Quick Q: Why would one use nested classes in C++?

Quick A: To hide implementation details

Recently on SO:

Why would one use nested classes in C++?

Nested classes are cool for hiding implementation details

List:

class List
{
    public:
        List(): head(NULL), tail(NULL) {}
    private:
        class Node
        {
              public:
                  int   data;
                  Node* next;
                  Node* prev;
        };
    private:
        Node*     head;
        Node*     tail;
};

Here I don't want to expose Node as other people may decide to use the class and that would hinder me from updating my class as anything exposed is part of the public API and must be maintained forever. By making the class private, I not only hide the implementation I am also saying this is mine and I may change it at any time so you can not use it.

Look at std::list or std::map they all contain hidden classes (or do they?). The point is they may or may not, but because the implementation is private and hidden the builders of the STL were able to update the code without affecting how you used the code, or leaving a lot of old baggage laying around the STL because they need to maintain backwards compatibility with some fool who decided they wanted to use the Node class that was hidden inside <list>.

Overload 141 is now available -- ACCU

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

Overload 141

From the index:

'Speedy Gonzales' Serializing (Re)Actors via Allocators

Polymorphism in C++ - A Type Compatibility View

Open Source - And Still Deceiving Programmers

C++11 (and Beyond) Exception Support