October 2017

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.

CppCast Episode 124: Build Systems and Modules with Isabella Muerte

Episode 124 of CppCast the only podcast for C++ developers by C++ developers. In this episode Rob and Jason are joined by Isabella Muerte to talk about her recent talk at CppCon 2017 where she discussed some of her concerns with the Modules TS.

CppCast Episode 124: Build Systems and Modules with Isabella Muerte

by Rob Irving and Jason Turner

About the interviewee:

Isabella Muerte is a C++ Bruja and Build System Trash Goblin. She taught herself to program by writing a build system and immediately regretting the decision. Her first computer ran Windows Millennium Edition and her parents forbade her from upgrading to anything else for 5 years. She is still bitter about this. In her spare time, she is into open source software, tattoos, computer keyboards, and making fake cover bands like 'Rage Against the Abstract Machine'

Bjarne Stroustrup Interview at cppcon 2017—Steve Carroll

Another great interview!

Bjarne Stroustrup Interview at cppcon 2017

by Steve Carroll

From the video:

We are back with footage from cppcon 2017, which occurred in Bellevue, Washington! In this special GoingNative episode, our host Steve Carroll chats with Gabriel Dos Reis and Bjarne Stroustrup, the creator of C++, about his keynote this year on the topic of education: Learning and Teaching Modern C++ (available on YouTube).

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>.