std:: add_pointer
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더에 정의됨
<type_traits>
|
||
|
template
<
class
T
>
struct add_pointer ; |
(C++11부터) | |
만약
T
가
referenceable type
이거나 (possibly cv-qualified)
void
인 경우, 제공되는 멤버 typedef
type
은
typename
std::
remove_reference
<
T
>
::
type
*
입니다.
그렇지 않으면, 제공된 멤버 typedef
type
는
T
입니다.
프로그램이
std::add_pointer
에 대한 특수화를 추가하는 경우, 동작은 정의되지 않습니다.
목차 |
중첩 타입
| 이름 | 정의 |
type
|
위에서 결정된 대로 |
헬퍼 타입
|
template
<
class
T
>
using add_pointer_t = typename add_pointer < T > :: type ; |
(C++14 이후) | |
가능한 구현
namespace detail { template<class T> struct type_identity { using type = T; }; // 또는 std::type_identity 사용 (C++20부터) template<class T> auto try_add_pointer(int) -> type_identity<typename std::remove_reference<T>::type*>; // 일반적인 경우 template<class T> auto try_add_pointer(...) -> type_identity<T>; // 특수한 경우 (std::remove_reference<T>::type*를 형성할 수 없음) } // namespace detail template<class T> struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {}; |
예제
#include <iostream> #include <type_traits> template<typename F, typename Class> void ptr_to_member_func_cvref_test(F Class::*) { // F는 "가증한 함수 타입(abominable function type)"입니다 using FF = std::add_pointer_t<F>; static_assert(std::is_same_v<F, FF>, "FF should be precisely F"); } struct S { void f_ref() & {} void f_const() const {} }; int main() { int i = 123; int& ri = i; typedef std::add_pointer<decltype(i)>::type IntPtr; typedef std::add_pointer<decltype(ri)>::type IntPtr2; IntPtr pi = &i; std::cout << "i = " << i << '\n'; std::cout << "*pi = " << *pi << '\n'; static_assert(std::is_pointer_v<IntPtr>, "IntPtr should be a pointer"); static_assert(std::is_same_v<IntPtr, int*>, "IntPtr should be a pointer to int"); static_assert(std::is_same_v<IntPtr2, IntPtr>, "IntPtr2 should be equal to IntPtr"); typedef std::remove_pointer<IntPtr>::type IntAgain; IntAgain j = i; std::cout << "j = " << j << '\n'; static_assert(!std::is_pointer_v<IntAgain>, "IntAgain should not be a pointer"); static_assert(std::is_same_v<IntAgain, int>, "IntAgain should be equal to int"); ptr_to_member_func_cvref_test(&S::f_ref); ptr_to_member_func_cvref_test(&S::f_const); }
출력:
i = 123 *pi = 123 j = 123
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2101 | C++11 |
T
가
함수 타입
이면서
cv
또는
ref
한정자를 가질 경우 프로그램이 비정형이었음
|
이 경우 생성되는 타입은
T
임
|
참고 항목
|
(C++11)
|
타입이 포인터 타입인지 검사합니다
(클래스 템플릿) |
|
(C++11)
|
주어진 타입에서 포인터를 제거합니다
(클래스 템플릿) |