BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_addlvaluereference

Detailed Description

Outline

Purpose

Provide a compile-time type transformation to lvalue reference.

Classes

See also
bslmf_addrvaluereference, bslmf_removereference

Description

This component defines a meta-function, bsl::add_lvalue_reference, that may be used to transform a type to its lvalue reference type. An lvalue, as defined in C++11 standard [basic.lval], is an expression that designates a function or an object.

bsl::add_lvalue_reference and bsl::add_lvalue_reference_t meet the requirements of the add_lvalue_reference template defined in the C++11 standard [meta.trans.ref].

Usage

In this section we show intended use of this component.

Example 1: Transforming Types to Lvalue Reference Types

Suppose that we want to transform a set of types to their lvalue reference types.

Now, we instantiate the bsl::add_lvalue_reference template for each of these types, and use the bsl::is_same meta-function to assert the type static data member of each instantiation:

assert(true ==
assert(false ==
assert(true ==
#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
assert(true ==
#endif
t_TYPE & type
This typedef defines the return type of this meta function.
Definition bslmf_addlvaluereference.h:129
Definition bslmf_issame.h:146

Finally, if the current compiler supports alias templates C++11 feature, we instantiate the bsl::add_lvalue_reference_t template for the same set of types, and use the bsl::is_same meta-function to assert the resultant type of each instantiation:

#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
assert(true ==
(bsl::is_same<bsl::add_lvalue_reference_t<int>, int& >::value));
assert(false ==
(bsl::is_same<bsl::add_lvalue_reference_t<int>, int >::value));
assert(true ==
(bsl::is_same<bsl::add_lvalue_reference_t<int&>, int& >::value));
#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
assert(true ==
(bsl::is_same<bsl::add_lvalue_reference_t<int&&>, int&&>::value));
#endif
#endif

Note, that the rvalue reference used above is a feature introduced in the C++11 standard and may not be supported by all compilers.

Also note that the bsl::add_lvalue_reference_t avoids the ::type suffix and typename prefix when we want to use the result of bsl::add_lvalue_reference meta-function in templates.