An optional member of an optional object

My colleague told me that boost::optional suffers from missing the ability of building an optional of a member from an optional of its parent object.

Let’s consider the following code:

The intent is to get an uninitialized optional of the member type (int) when the optional of the its parent (Foo) is uninitialized and an initialized optional when its parent is initialized. That works, but remains repetitive. We could reduce like this:

But this code still repeats the type (int) of the member we want to capture and requires some code maintenance if one changes its type. One way to overcome this would be:

But there are still repetitions and remains ugly for the programmer’s eye.

At the end, one would like to write something like this:

But the evaluation of foo->bar will end up with an undefined behaviour when foo is uninitialized. So let’s consider the following:

It cannot be named make_optional, because it could collide with boost::make_optional and trying to resolve it by checking whether the second argument is a callable is a waste of time, because it would prevent the ability of constructing an optional of a callable.

make_optional_eval is simple and very flexible. There is no constraint between the condition and the expression. You can also invoke a method of an object and you can even call sub objects.

 

This entry was posted in boost, C++, C++11, optional. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *