table of contents
std::add_lvalue_reference,std::add_rvalue_reference(3) | C++ Standard Libary | std::add_lvalue_reference,std::add_rvalue_reference(3) |
NAME¶
std::add_lvalue_reference,std::add_rvalue_reference - std::add_lvalue_reference,std::add_rvalue_reference
Synopsis¶
Defined in header <type_traits>
template< class T > (1) (since C++11)
struct add_lvalue_reference;
template< class T > (2) (since C++11)
struct add_rvalue_reference;
Creates a 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¶
template< class T >
using add_lvalue_reference_t = typename (since C++14)
add_lvalue_reference<T>::type;
template< class T >
using add_rvalue_reference_t = typename (since C++14)
add_rvalue_reference<T>::type;
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&&
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¶
namespace detail {
template <class T>
struct type_identity { using type = T; }; // or use std::type_identity
(since C++20)
template <class T> // Note that `cv void&` is a substitution
failure
auto try_add_lvalue_reference(int) -> type_identity<T&>;
template <class T> // Handle T = cv void case
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¶
// 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;
using voidref = std::add_lvalue_reference_t<void>;
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';
std::cout << std::is_reference_v<voidref> << '\n';
}
Output:¶
false
true
true
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
These transformation traits were
LWG 2101 C++11 required Produce cv-/ref-qualified
to produce reference to function types themselves.
cv-/ref-qualified function types.
See also¶
is_reference checks if a type is either a lvalue reference or
rvalue reference
(C++11) (class template)
remove_reference removes a reference from the given type
(C++11) (class template)
remove_cvref combines std::remove_cv and std::remove_reference
(C++20) (class template)
2022.07.31 | http://cppreference.com |