On this page
std::add_lvalue_reference, std::add_rvalue_reference
Defined in header <type_traits> |
||
---|---|---|
|
(1) | (since C++11) |
|
(2) | (since C++11) |
Creates an lvalue or rvalue reference type of T
.
1) If
T
is a function type that has no cv- or ref- qualifier or an object type, 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 a function type that has no cv- or ref- qualifier or an object type, provides a member typedef type
which is T&&
, otherwise type
is T
.
The behavior of a program that adds specializations for any of the templates described on this page is undefined.
Member types
Name | Definition |
---|---|
type |
reference to T , or T if not allowed |
Helper types
|
(since C++14) | |
|
(since C++14) |
Notes
These type transformations honor reference collapse rules:
std::add_lvalue_reference<T&>::type
isT&
std::add_lvalue_reference<T&&>::type
isT&
std::add_rvalue_reference<T&>::type
isT&
std::add_rvalue_reference<T&&>::type
isT&&
The major difference to directly using T&
is that std::add_lvalue_reference<void>::type
is void
, while void&
leads to a compilation error.
Possible implementation
|
Example
#include <type_traits>
int main()
{
using non_ref = int;
using l_ref = typename std::add_lvalue_reference_t<non_ref>;
using r_ref = typename std::add_rvalue_reference_t<non_ref>;
using void_ref = std::add_lvalue_reference_t<void>;
static_assert
( std::is_lvalue_reference_v<non_ref> == false
&& std::is_lvalue_reference_v<l_ref> == true
&& std::is_rvalue_reference_v<r_ref> == true
&& std::is_reference_v<void_ref> == false
);
}
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
(C++11)
|
checks if a type is either an lvalue reference or rvalue reference (class template) |
(C++11)
|
removes a reference from the given type (class template) |
(C++20)
|
combines std::remove_cv and std::remove_reference (class template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/add_reference