Articles & Books

Quick Q: Why two null constructors for std::unique_ptr?

Quick A: The nullptr_t constructor was added later.

Recently on SO:

Why two null constructors for std::unique_ptr?

For (1), consider that it ensures that both the no-arg constructor unique_ptr() and null-pointer constructor unique_ptr(nullptr_t) have the same compile-time guarantees, i.e. both are constexpr. We can see the difference in §20.8.1.2:

constexpr unique_ptr() noexcept;
explicit unique_ptr(pointer p) noexcept;
...
constexpr unique_ptr(nullptr_t) noexcept
: unique_ptr() { }

Why the two were not combined into a single constructor with a default value is likely historical contingency.

With regards to (2), why we should care about constexpr despite having a non-trivial destructor, consider the answer given here:

constexpr constructors can be used for constant initialization, which, as a form of static initialization, is guaranteed to happen before any dynamic initialization takes place.

For example, given a global std::mutex:

std::mutex mutex;

In a conforming implementation (read: not MSVC), constructors of other objects can safely lock and unlock mutex, becuase std::mutex's constructor is constexpr.

Parameter or Argument?--Malte Langkabel

Did you know the difference?

Parameter or Argument?

by Malte Langkabel

From the article:

I often hear people getting confused when talking about parameters and arguments. That confusion grows even stronger when one of them knows the difference but the other one doesn’t. So let’s shed some light on this issue and spread the knowledge! Programming involves talking to each other but that doesn’t have to be more painful than it already is wink

Zeroing Memory is Hard (VC++ 2015 arrays)--Bruce Dawson

Here is a curious behaviour:

Zeroing Memory is Hard (VC++ 2015 arrays)

by Bruce Dawson

From the article:

Quick, what’s the difference between these two C/C++ definitions of initialized local variables?

char buffer[32] = { 0 };
char buffer[32] = {};

One difference is that the first is legal in C and C++, whereas the second is only legal in C++.

Okay, so let’s focus our attention on C++. What do these two definitions mean?

Meeting C++ 2016: closing keynote & full schedule

With the announcement of the closing keynote the full schedule for Meeting C++ 2016 stands!

Closing keynote & full schedule of Meeting C++ 2016

by Jens Weller

From the article:

Since mid of June the program of the 5th Meeting C++ conference was taking shape. With the selection of the talks it was also clear in which tracks they go, so that the schedule it self was almost ready, except a last detail: the closing keynote.

The closing keynote will be held by Louis Dionne on "C++ metaprogramming: evolution and future directions".

Red Hat at the ISO C++ Standards Meeting (June 2016, Oulu): Library--JWAKELY

C++ continues to evolve:

Red Hat at the ISO C++ Standards Meeting (June 2016, Oulu): Library

by JWAKELY

From the article:

The recent WG21 meeting in Oulu, Finland, was an especially busy one for the Library Working Group. Every day was spent working through the list of proposals intended for inclusion in C++17, and we also had three “evening” sessions that ran well past the evening (until nearly midnight, although the sun was still up to trick us into working late)...

 

Red Hat at the ISO C++ Standards Meeting (June 2016, Oulu): Core Language--Jason Merrill

C++ continues to evolve:

Red Hat at the ISO C++ Standards Meeting (June 2016, Oulu): Core Language

by Jason Merrill

From the article:

It was quite a trek to get to Oulu, Finland for the June 2016 C++ Standards Committee meeting, but we were warmly received and the meeting went well once we arrived. We had very pleasant weather most of the week, and it was fun to experience the midnight sun, even though it played havoc with my sleep schedule.

Red Hat at the ISO C++ Standards Meeting (June 2016, Oulu): Parallelism and Concurrency--Torvald Rie

C++ continues to evolve:

Red Hat at the ISO C++ Standards Meeting (June 2016, Oulu): Parallelism and Concurrency

by Torvald Riegel

From the article:

Several Red Hat engineers recently attended the JTC1/SC22/WG21 C++ Standards Committee meetings in Oulu, Finland.  This post focuses on the sessions of SG1 (the standards committee sub-group 1 – for concurrency and parallelism) as well as on coroutines-related sessions. Jason already gave an overview of the meeting in his post.

Quick Q: How to express constness of a forwarding reference?

Quick A: To express constness, a const reference is what is needed.

Recently on SO:

How to express constness of a forwarding reference?

How can I express that f does not modify its parameter?

If the function doesn't modify its parameter then there is no benefit to using a forwarding reference. You use a forwarding reference when you want to forward the parameter on to some other function which cares whether the argument is an lvalue or an rvalue (because maybe there are separate overloads for lvalues and rvalues, and the rvalue one can be more efficient).

If your function doesn't modify the argument then it shouldn't need to care whether it has an lvalue or rvalue, so it can just take const T& unconditionally. That can bind to both lvalues and rvalues, and promises not to modify the parameter.

template<typename T>
void f(const T& t) { ... }