std:: unwrap_reference, std:: unwrap_ref_decay
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더에 정의됨
<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&
로 변환합니다.
T
가
std::reference_wrapper
의 특수화(specialization)라면, 이를 언래핑(unwraps)합니다; 그렇지 않으면
T
는 그대로 유지됩니다.
프로그램이 이 페이지에 설명된 템플릿들 중 어느 하나에 대해 특수화를 추가하는 경우, 동작은 정의되지 않습니다.
목차 |
중첩 타입
| 유형 | 정의 |
type
|
(1)
|
헬퍼 타입
|
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
참고 항목
|
(C++11)
|
CopyConstructible
및
CopyAssignable
참조 래퍼
(클래스 템플릿) |
인자 타입들에 의해 결정된 타입의
pair
객체를 생성함
(함수 템플릿) |
|
|
(C++11)
|
인자 타입들에 의해 정의된 타입의
tuple
객체를 생성함
(함수 템플릿) |