Why is std :: reference_wrapper not constructive by default?

I feel that avoiding the constructive constructor std::reference_wrapper<T>makes it much harder to use, although using the default built reference_wrapper raises a runtime exception.

Nevertheless, it is reference_wrapperperfectly copied, so its value can always be changed, so why does it not have a null reference by default? This makes many use cases much easier and with it, the proposed one is observer_ptrno longer needed - why do we need redundancy? By default, the constructor reference_wrapperwill manage them all!

Thoughts?

+4
source share
1 answer

However, reference_wrapper is perfectly copied, so its value can always be changed, so why by default it has no null reference?

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

std::reference_wrappercreated as a wrapper for the link, namely. It should behave like a link, otherwise it will not be a wrapper, but something else. For him, there are precedents that may break if you allow him a default value. If you need to have a null value, use either another, such as a pointer, or live with link restrictions.

In addition, you yourself indicate the reason:

[...] although using the default built reference_wrapper raises an exception at runtime.

? std::reference_wrapper, , -, . , , , .

[...]

, . . , , ..

, std::reference_wrapper - , , . , std::observer_ptr, .

+7

Source: https://habr.com/ru/post/1666209/


All Articles