BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_unwrapreference

Detailed Description

Provide a meta-function to unwrap reference wrappers.

Outline

Purpose

Provide a meta-function to unwrap reference wrappers.

Classes

Canonical Header

bsl_functional.h, bsl_type_traits.h

See also
bslmf_referencewrapper, bslmf_unwrap_ref_decay

Description

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].

Usage

In this section we show intended use of this component.

Example 1: Unwrap Reference Wrapped Types

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:

typedef bsl::reference_wrapper<int *> WrappedType;
typedef int *NotWrappedType;
Definition bslmf_referencewrapper.h:182

Next, we create types that are references if they were wrapped:

typedef bsl::unwrap_reference<WrappedType>::type UnwrappedWrapped;
typedef bsl::unwrap_reference<NotWrappedType>::type UnwrappedNotWrapped;

Finally we can verify that the wrapped type became a reference, while the other type is unchanged:

Definition bslmf_issame.h:146

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.