std::add_lvalue_reference, std::add_rvalue_reference

Header: <type_traits>

Creates an lvalue or rvalue reference type of T.

# Declarations

template< class T >
struct add_lvalue_reference;

(since C++11)

template< class T >
struct add_rvalue_reference;

(since C++11)

# Notes

The major difference to directly using T& or T&& is that T can be a non-referenceable type. For example, std::add_lvalue_reference::type is void, while void& leads to a compilation error.

# Example

#include <type_traits>
 
using non_ref = int;
static_assert(std::is_lvalue_reference_v<non_ref> == false);
 
using l_ref = std::add_lvalue_reference_t<non_ref>;
static_assert(std::is_lvalue_reference_v<l_ref> == true);
 
using r_ref = std::add_rvalue_reference_t<non_ref>;
static_assert(std::is_rvalue_reference_v<r_ref> == true);
 
using void_ref = std::add_lvalue_reference_t<void>;
static_assert(std::is_reference_v<void_ref> == false);
 
int main() {}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2101C++11the program was ill-formed if T is a function type with cv or refthe type produced is T in this case

# See also