Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to combine optional with reference_wrapper?

It occurred to me that in C++ it is possible to use the type std::optional<std::reference_wrapper<T>>. An object of this type is essentially a reference to an object of type T or a null value, i.e., pretty much a pointer. My questions:

  1. Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T*?

  2. Is there any practical difference? Are there situations where it might be advisable to choose std::optional<std::reference_wrapper<T>> over T*?

like image 919
Bobby Avatar asked Feb 10 '18 13:02

Bobby


People also ask

Can optional hold a reference?

In a word, optional references share some semantics with pointers: they can point to something like a normal reference, and they can also point to nothing when they're a null optional. But they only represent handles and don't do pointer arithmetics and such low-level features.

What is the use of reference_wrapper?

A reference_wrapper can be used to store references in standard containers, and to pass objects by reference to std::bind . The type Ty must be an object type or a function type, or a static assert fails at compile time. The helper functions std::ref and std::cref can be used to create reference_wrapper objects.

Is std :: optional a pointer?

Thus, an optional object models an object, not a pointer, even though operator*() and operator->() are defined. When an object of type optional<T> is contextually converted to bool , the conversion returns true if the object contains a value and false if it does not contain a value.

Does STD optional allocate?

What's more, std::optional doesn't need to allocate any memory on the free store. std::optional is a part of C++ vocabulary types along with std::any , std::variant and std::string_view .


2 Answers

Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T*?

std::optional<>, as the name already suggest, is meant to be used when we could have a value or might not have any value at all.

The equivalent of having no value for a T* object would be assigning nullptr to it, i.e.: the pointer will point to nowhere, as opposed to somewhere (or even anywhere, i.e.: uninitialized). It can be said that std::optional<> exports the concept of nullptr for pointers to any arbitrary type. So, I would say they are conceptually very similar, being the std::option<> approach a kind of generalization.

Is there any practical difference? Are there situations where it might be advisable to choose std::optional<std::reference_wrapper<T>> over T*?

I can think of the size. std::optional<> contains an internal flag for indicating the presence/absence of a value, whereas for T* the nullptr is encoded directly as one of the values the pointer can store. So a std::optional<std::reference_wrapper<T>> object will be larger than a T*.

When it comes to safety, unlike T*, std::optional<> provides the member function value() which throws an exception if there is no value (it provides as well as the unsafe operator*() as T* does).

Also, using std::optional<std::reference_wrapper<T>> instead of T* , for example, as a function's return value may indicate in a more explicit way that there might be no value at all.

like image 153
ネロク・ゴ Avatar answered Oct 05 '22 13:10

ネロク・ゴ


The main difference between std::optional<std::reference_wrapper<T>> and T* is that with T* you have to think about who owns the memory that is pointed to.

If a function returns T* you have to know if you are responsible for freeing the memory or someone else is. That's not something you have to be concerned with when it's a reference.

like image 36
TimK Avatar answered Oct 05 '22 14:10

TimK