Namespaces
Variants

std:: add_lvalue_reference, std:: add_rvalue_reference

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
add_lvalue_reference add_rvalue_reference
(C++11) (C++11)

Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
헤더 파일에 정의됨 <type_traits>
template < class T >
struct add_lvalue_reference ;
(1) (C++11부터)
template < class T >
struct add_rvalue_reference ;
(2) (C++11부터)

T 의 lvalue 또는 rvalue 참조 타입을 생성합니다.

타입 특성 중첩 타입 type 이 참조하는 타입
T 참조 가능 타입 인 경우 T 가 참조 가능 타입이 아닌 경우
(1) T& [1] T
(2) T&& [2]
  1. 이 규칙은 reference collapsing 의 의미론을 반영합니다.
  2. 이 규칙은 reference collapsing 의 의미론을 반영합니다. std :: add_rvalue_reference < T & > :: type T& 이며, 이는 rvalue reference 타입이 아닙니다.

프로그램이 이 페이지에 설명된 템플릿들 중 어느 하나에 대해 특수화를 추가하는 경우, 동작은 정의되지 않습니다.

목차

중첩 타입

이름 정의
type 위와 같이 결정됨

헬퍼 타입

template < class T >
using add_lvalue_reference_t = typename add_lvalue_reference < T > :: type ;
(C++14부터)
template < class T >
using add_rvalue_reference_t = typename add_rvalue_reference < T > :: type ;
(C++14부터)

참고 사항

직접 T& 또는 T&& 를 사용하는 것과의 주요 차이점은 T 참조 불가능한 타입 일 수 있다는 것입니다. 예를 들어, std :: add_lvalue_reference < void > :: type void 이지만, void & 는 컴파일 오류를 유발합니다.

가능한 구현

namespace detail
{
    template<class T>
    struct type_identity { using type = T; }; // 또는 std::type_identity 사용 (C++20부터)
    template<class T> // "cv void&"는 치환 실패임에 유의
    auto try_add_lvalue_reference(int) -> type_identity<T&>;
    template<class T> // T = cv void 경우 처리
    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)) {};

예제

#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() {}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2101 C++11 T 함수 타입 이면서 cv 또는 ref 인 경우 프로그램이 비정형이었음 이 경우 생성되는 타입은 T

참고 항목

해당 타입이 lvalue reference 또는 rvalue reference 인지 검사합니다
(class template)
주어진 타입에서 reference를 제거합니다
(class template)
std::remove_cv std::remove_reference 를 결합합니다
(class template)