Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::reference_wrapper not default constructible?

I feel that preventing std::reference_wrapper<T> to be default constructible makes it much harder to use, even though using a default constructed reference_wrapper causes a runtime exception.

However, a reference_wrapper is perfectly copyable, so it's value can always be changed, so why preventing it to have the null reference by default? It makes many usage cases much simpler and with it, the proposed observer_ptr is not needed anymore - why need the redundancy? A default constructible reference_wrapper would rule them all!

Thoughts?

like image 712
Dejavu Avatar asked Jan 09 '17 05:01

Dejavu


People also ask

Why use std reference_ wrapper?

std::reference_wrapperIt is frequently used as a mechanism to store references inside standard containers (like std::vector) which cannot normally hold references. Specifically, std::reference_wrapper is a CopyConstructible and CopyAssignable wrapper around a reference to object or reference to function of type T .

Can reference_wrapper be null?

References cannot be null and cannot rebind, std::reference_wrapper cannot be null but can rebind, Pointers can be null and can rebind (and can do low-level address manipulations), boost optional references can be null and can rebind (but are incompatible with std::optional ).

How does std:: reference_ wrapper work?

An std::reference_wrapper is a copyable and assignable object that emulates a reference. Contrary to its name, it does not wrap a reference. It works by encapsulating a pointer (T*) and by implicitly converting to a reference (T&).


1 Answers

However, a reference_wrapper is perfectly copyable, so it's value can always be changed, so why preventing it to have the null reference by default?

What would be the point of std::reference_wrapper to have a null value? If you need to have a null value, just use a pointer :P

std::reference_wrapper is built as a wrapper to a reference, that's it. It must behave like a reference would, or else it wouldn't be a wrapper but something else. There are use-cases for it, which would possibly break if you allow a default value for it. If you need to have a null value, either use something else, like a pointer, or live with the limitations of references.

Also, you state a reason yourself:

[...] even though using a default constructed reference_wrapper causes a runtime exception.

What's the point in having a reference to nothing? Having a std::reference_wrapper implies that it refers to something, just like a reference would. Adding a null value would just mean additional code every time the reference is used to see if it is null or not.

It makes many usage cases much simpler [...]

Yes, maybe. But it'll make other use-cases much harder. You'll need to check if the wrapper is valid or not, don't forget to initialize it when used as a member with a default constructor, and more.

In short, std::reference_wrapper is just a wrapper to a reference, and as such cannot behave as something else than just a plain reference. Everything has an advantage and a disadvantage, here, you might need std::observer_ptr, but in other cases, you don't need to do any checks.

like image 51
Rakete1111 Avatar answered Sep 23 '22 08:09

Rakete1111