std::remove_pointer

< cpp‎ | types
在标头 <type_traits> 定义
template< class T >
struct remove_pointer;
(C++11 起)

提供成员 typedef type,它是 T 所指向的类型,或若 T 不是指针,则 typeT 相同。

如果程序添加了 std::remove_pointer 的特化,那么行为未定义。

成员类型

名称 定义
type T 所指向的类型,或若 T 不是指针则为 T

辅助类型

template< class T >
using remove_pointer_t = typename remove_pointer<T>::type;
(C++14 起)

可能的实现

template<class T> struct remove_pointer { typedef T type; };
template<class T> struct remove_pointer<T*> { typedef T type; };
template<class T> struct remove_pointer<T* const> { typedef T type; };
template<class T> struct remove_pointer<T* volatile> { typedef T type; };
template<class T> struct remove_pointer<T* const volatile> { typedef T type; };

示例

#include <type_traits>
 
static_assert
(
    std::is_same_v<int, int> == true &&
    std::is_same_v<int, int*> == false &&
    std::is_same_v<int, int**> == false &&
    std::is_same_v<int, std::remove_pointer_t<int>> == true &&
    std::is_same_v<int, std::remove_pointer_t<int*>> == true &&
    std::is_same_v<int, std::remove_pointer_t<int**>> == false &&
    std::is_same_v<int, std::remove_pointer_t<int* const>> == true &&
    std::is_same_v<int, std::remove_pointer_t<int* volatile>> == true &&
    std::is_same_v<int, std::remove_pointer_t<int* const volatile>> == true
);
 
int main() {}

参阅

(C++11)
检查类型是否为指针类型
(类模板)
(C++11)
对给定类型添加一层指针
(类模板)