Namespaces
Variants

std:: remove_pointer

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < class T >
struct remove_pointer ;
(C++11 起)

提供成员类型定义 type ,该类型为 T 所指向的类型;若 T 不是指针类型,则 type T 相同。

如果程序为 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)
检查类型是否为指针类型
(类模板)
为给定类型添加指针
(类模板)