std::add_lvalue_reference, std::add_rvalue_reference - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::add_lvalue_reference, std::add_rvalue_reference

From cppreference.com
< cpp‎ | types

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 or function that isn't cv- or ref- qualified (since C++17), 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 or function that isn't cv- or ref- qualified (since C++17), provides a member typedef `type` which is `T&&`, otherwise `type` is `T`.

### [edit] Member types

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

### [edit] 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)

### [edit] 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&&

### [edit] Example

Run this code

    #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

### [edit] See also

is_reference (C++11) | checks if a type is either lvalue reference or rvalue reference (class template) [edit]
remove_reference (C++11) | removes reference from the given type (class template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/types/add_reference&oldid=81500"