What is the purpose of std::add_lvalue_reference
and std::add_rvalue_reference
?
It seems that using T &
/T &&
does the same, as this successfully compiles:
#include <utility>
int main() {
{ using T = int; static_assert(std::is_same_v<std::add_lvalue_reference_t<T>, T &>); };
{ using T = int &; static_assert(std::is_same_v<std::add_lvalue_reference_t<T>, T &>); };
{ using T = int &&; static_assert(std::is_same_v<std::add_lvalue_reference_t<T>, T &>); };
{ using T = int; static_assert(std::is_same_v<std::add_rvalue_reference_t<T>, T &&>); };
{ using T = int &; static_assert(std::is_same_v<std::add_rvalue_reference_t<T>, T &&>); };
{ using T = int &&; static_assert(std::is_same_v<std::add_rvalue_reference_t<T>, T &&>); };
}
void&
is ill-formed. std::add_lvalue_reference<void>
is void
.
In general, add_lvalue_reference
does not add reference to types if it is not possible. Per [meta.trans.ref]:
template <class T> struct add_lvalue_reference;
If T names a referenceable type then the member typedef
type
namesT&
; otherwise,type
namesT
. [ Note: This rule reflects the semantics of reference collapsing ([dcl.ref]). — end note ]
What is a referenceable type? Per [defns.referenceable], a referenceable type is
an object type, a function type that does not have cv-qualifiers or a ref-qualifier, or a reference type [ Note: The term describes a type to which a reference can be created, including reference types. — end note ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With