std::add_lvalue_reference, std::add_rvalue_reference

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Endian
(C++20)
Constant evaluation context
Supported operations
Relationships and property queries
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
add_lvalue_referenceadd_rvalue_reference
(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(until C++20)(C++17)
 
Defined in header <type_traits>
template< class T >
struct add_lvalue_reference;
(1) (since C++11)
template< class T >
struct add_rvalue_reference;
(2) (since C++11)

Creates a lvalue or rvalue reference type of T.

1) If T is an object type or a function type that has no cv- or ref- qualifier, provides a member typedef type which is T&. If T is an rvalue reference to some type U, then type is U&. Otherwise, type is T.
2) If T is an object type or a function type that has no cv- or ref- qualifier, provides a member typedef type which is T&&, otherwise type is T.

Member types

Name Definition
type reference to T, or T if not allowed

Helper types

template< class T >
using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
(since C++14)
template< class T >
using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
(since C++14)

Notes

These type transformations honor reference collapse rules:

std::add_lvalue_reference<T&>::type is T&
std::add_lvalue_reference<T&&>::type is T&
std::add_rvalue_reference<T&>::type is T&
std::add_rvalue_reference<T&&>::type is T&&

Possible implementation

namespace detail {
 
template <class T>
struct type_identity { using type = T; }; // or use std::type_identity (since C++20)
 
template <class T>
auto try_add_lvalue_reference(int) -> type_identity<T&>;
template <class T>
auto try_add_lvalue_reference(...) -> type_identity<T>;
 
template <class T>
auto try_add_rvalue_reference(int) -> type_identity<T&&>;
template <class T>
auto try_add_rvalue_reference(...) -> type_identity<T>;
 
} // namespace detail
 
template <class T>
struct add_lvalue_reference : decltype(detail::try_add_lvalue_reference<T>(0)) {};
 
template <class T>
struct add_rvalue_reference : decltype(detail::try_add_rvalue_reference<T>(0)) {};

Example

#include <iostream>
#include <type_traits>
 
int main() {
   using nonref = int;
   using lref = typename std::add_lvalue_reference<nonref>::type;
   using rref = typename std::add_rvalue_reference<nonref>::type;
 
   std::cout << std::boolalpha;
   std::cout << std::is_lvalue_reference<nonref>::value << '\n';
   std::cout << std::is_lvalue_reference<lref>::value << '\n';
   std::cout << std::is_rvalue_reference<rref>::value << '\n';
}

Output:

false
true
true

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2101 C++11 These transformation traits were required
to produce reference to cv-/ref-qualified function types.
Produce cv-/ref-qualified function types themselves.

See also

checks if a type is either a lvalue reference or rvalue reference
(class template)
removes a reference from the given type
(class template)
combines std::remove_cv and std::remove_reference
(class template)