Namespaces
Variants

std::unwrap_reference, std::unwrap_ref_decay

cppreference.net에서 가져옴
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐기됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
Swap 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
함수 객체
함수 호출
(C++17)(C++23)
항등 함수 객체
(C++20)
투명 연산자 래퍼
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

이전 바인더 및 어댑터
(C++17까지*)
(C++17까지*)
(C++17까지*)
(C++17까지*)  
(C++17까지*)
(C++17까지*)(C++17까지*)(C++17까지*)(C++17까지*)
(C++20까지*)
(C++20까지*)
(C++17까지*)(C++17까지*)
(C++17까지*)(C++17까지*)

(C++17까지*)
(C++17까지*)(C++17까지*)(C++17까지*)(C++17까지*)
(C++20까지*)
(C++20까지*)
 
에 정의됨<functional>
template< class T >
struct unwrap_reference;
(1) (C++20부터)
template< class T >
struct unwrap_ref_decay;
(2) (C++20부터)

임의의 std::reference_wrapper를 해제합니다: std::reference_wrapper<U>를 U&로 변경합니다.

1) 만약 T가 std::reference_wrapper의 특수화이면, 이를 해제하고; 그렇지 않으면, T는 그대로 유지됩니다.
2) 만약 감소된(decayed) T가 std::reference_wrapper의 특수화이면, 이를 해제하고; 그렇지 않으면, T는 감소(decay)됩니다.

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

중첩 타입

유형 정의
type

(1) U& 만약 T 가 std:: reference_wrapper < U > 인 경우; T 그 외의 경우
(2) U& 만약 std:: decay_t < T > 가 std:: reference_wrapper < U > 인 경우; std:: decay_t < T > 그 외의 경우

헬퍼 타입

template < class T >
using unwrap_reference_t = unwrap_reference < T > :: type ;
(1) (C++20부터)
template < class T >
using unwrap_ref_decay_t = unwrap_ref_decay < T > :: type ;
(2) (C++20부터)

가능한 구현

template<class T>
struct unwrap_reference { using type = T; };
template<class U>
struct unwrap_reference<std::reference_wrapper<U>> { using type = U&; };
template<class T>
struct unwrap_ref_decay : std::unwrap_reference<std::decay_t<T>> {};

참고 사항

std::unwrap_ref_decay 는 std::make_pair 와 std::make_tuple 에서 사용되는 것과 동일한 변환을 수행합니다.

기능 테스트 매크로 값 표준 기능
__cpp_lib_unwrap_ref 201811L (C++20) std::unwrap_ref_decay 및 std::unwrap_reference

예제

#include <cassert>
#include <functional>
#include <iostream>
#include <type_traits>
int main()
{
    static_assert(std::is_same_v<std::unwrap_reference_t<int>, int>);
    static_assert(std::is_same_v<std::unwrap_reference_t<const int>, const int>);
    static_assert(std::is_same_v<std::unwrap_reference_t<int&>, int&>);
    static_assert(std::is_same_v<std::unwrap_reference_t<int&&>, int&&>);
    static_assert(std::is_same_v<std::unwrap_reference_t<int*>, int*>);
    {
        using T = std::reference_wrapper<int>;
        using X = std::unwrap_reference_t<T>;
        static_assert(std::is_same_v<X, int&>);
    }
    {
        using T = std::reference_wrapper<int&>;
        using X = std::unwrap_reference_t<T>;
        static_assert(std::is_same_v<X, int&>);
    }
    static_assert(std::is_same_v<std::unwrap_ref_decay_t<int>, int>);
    static_assert(std::is_same_v<std::unwrap_ref_decay_t<const int>, int>);
    static_assert(std::is_same_v<std::unwrap_ref_decay_t<const int&>, int>);
    {
        using T = std::reference_wrapper<int&&>;
        using X = std::unwrap_ref_decay_t<T>;
        static_assert(std::is_same_v<X, int&>);
    }
    {
        auto reset = []<typename T>(T&& z)
        {
        //  x = 0; // 오류: T가 reference_wrapper<>인 경우 작동하지 않음
            // 일반 타입에 대해 T&&를 T&로 변환
            // reference_wrapper<U>에 대해 T&&를 U&로 변환
            decltype(auto) r = std::unwrap_reference_t<T>(z);
            std::cout << "r: " << r << '\n';
            r = 0; // OK, r은 참조 타입을 가짐
        };
        int x = 1;
        reset(x);
        assert(x == 0);
        int y = 2;
        reset(std::ref(y));
        assert(y == 0);
    }
}

출력:

r: 1
r: 2

참고 항목

CopyConstructible 및 CopyAssignable 참조 래퍼
(클래스 템플릿)
인자 타입들에 의해 결정된 타입의 pair 객체를 생성함
(함수 템플릿)
(C++11)
인자 타입들에 의해 정의된 타입의 tuple 객체를 생성함
(함수 템플릿)