|
BDE 4.39.x Production Release
|
Provide a meta-function to unwrap reference wrappers.
Provide a meta-function to unwrap reference wrappers.
bsl_functional.h, bsl_type_traits.h
This component defines a meta-function bsl::unwrap_reference that may be used to unwrap a bsl::reference_wrapper (which is an alias of std::reference_wrapper when that exists) of some type U, resulting in U&. In case the specified type template argument is not a specialization of bsl::reference_wrapper the result is the type itself.
bsl::unwrap_reference meets the requirements of the unwrap_reference template defined in the C++20 standard [meta.trans.other].
In this section we show intended use of this component.
Suppose that we work in a programming environment where types may be presented wrapped in a bsl::reference_wrapper or its std equivalent. We would like to use a reference to the wrapped type, but use unwrapped types as they are. This is the exact use case for bsl::unwrap_reference.
First, we create types that represent both reference-wrapped, and normal type parameters:
Next, we create types that are references if they were wrapped:
Finally we can verify that the wrapped type became a reference, while the other type is unchanged:
Note, that (when available) the bsl::unwrap_reference_t avoids the ::type suffix and typename prefix when we want to use the result of the bsl::unwrap_reference meta-function in templates.