Namespaces
Variants

std:: unwrap_reference, std:: unwrap_ref_decay

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Reference wrappers
(C++11) (C++11)
unwrap_reference unwrap_ref_decay
(C++20) (C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
헤더에 정의됨 <type_traits>
헤더에 정의됨 <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 의 특수화(specialization)라면, 이를 언래핑(unwraps)합니다; 그렇지 않으면 T 는 그대로 유지됩니다.
2) 감소된 T std::reference_wrapper 의 특수화인 경우, 이를 언래핑합니다; 그렇지 않은 경우, T 는 감소됩니다.

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

목차

중첩 타입

유형 정의
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 객체를 생성함
(함수 템플릿)