Understand ranges better with the new Cartesian Product adaptor—Jonathan Boccara

The future explained:

Understand ranges better with the new Cartesian Product adaptor

by Jonathan Boccara

From the article:

A couple of days ago, the range-v3 library got a new component: the view::cartesian_product adaptor.

Understanding what this component does, and the thought process that went through its creation is easy and will let you have a better grasp of the range library. (Note that you could just as well understand all the following by looking at the zip adaptor. But cartesian_product is brand new, so let’s discover this one, in order to hit two birds with one stone)...

Quick Q: Prevent user from derive from incorrect CRTP base

Quick A: Make it impossible to construct a class of the wrong type.

Recently on SO:

Prevent user from derive from incorrect CRTP base

1) make all constructors of Base private (if there are no constructors, add one)

2) declare Derived template parameter as friend of Base

template <class Derived>
class Base

  Base(){}; // prevent undesirable inheritance making ctor private
  friend  Derived; // allow inheritance for Derived

public :

  void call ()
      static_cast<Derived *>(this)->call_impl();

After this it would be impossible to create any instances of the wrong inherited D2.

Quick Q: Conditionally acquire an std::mutex

Quick A: use a std::unique_lock with std::try_to_lock parameter in the constructor.

Recently on SO:

Conditionally acquire an std::mutex

It is actually unsafe to have a unique_lock accessible from multiple threads at the same time. I'm not familiar with the opencv portion of your question, so this answer is focused on the mutex/lock usage.

static std::mutex s_FAST_GPU_mutex;
   // Create a unique lock, attempting to acquire
   std::unique_lock<std::mutex> guard(s_FAST_GPU_mutex, std::try_to_lock);
   if (guard.owns_lock())
       guard.unlock(); // Or just let it go out of scope later

This attempts to acquire the lock, if it succeeds, uses FAST_GPU, and then releases the lock. If the lock was already acquired, then goes down the second branch, invoking FAST

Smelly std::pair and std::tuple—Arne Mertz

Do you use them badly?

Smelly std::pair and std::tuple

by Arne Mertz

From the article:

Depending on their use, std::pair and std::tuple can be code smells. That’s why we should be careful around these two.

Having a code smell is not a no-go, it’s more like a red flag. It’s one of those things that are not a problem themselves but rather a hint that there might be a less obvious problem hidden in the code.

Singleton revisited—Giuseppe Puoti

The use of Singleton is often justified by the will of being able to easily refer to a unique object from any point. Giuseppe Puoti describes an alternative:

Singleton revisited

by Giuseppe Puoti

From the article:

It happens quite often to deal with those annoying objects, used everywhere in your applications. This may be because of a poorly designed application but, sometime, it is simply an inevitable condition...

Reflections on the reflection proposals

Since the overview on the current papers for Kona, I wanted to know more about reflection...

Reflections on the reflection proposals

by Jens Weller

From the article

A few weeks ago I wrote a short overview over the most interesting papers for the current C++ Committee meeting in Kona, Hawaii. The big surprise was that there were many papers on reflection, while there already is a very detailed proposal for reflection.

With the C++ committee currently in Kona discussing lots of proposals, there will be some changes to the on going effort for reflection, but the current proposals are detailed enough to give an overview.

Generating Sequences

A virtual container.

Generating Sequences

By Anthony Williams

From the article:

I was having a discussion with my son over breakfast about C++ and Python, and he asked me if C++ had anything equivalent to Python's range() function for generating a sequence of integers. I had to tell him that no, the C++ standard library didn't supply such a function, but there were algorithms for generating sequences (std::generate and std::generate_n) into an existing container, and you could write something that would provide a "virtual" container that would supply a sequence as you iterated over it with range-for...