C++标准模板(STL)- 类型支持 (类型修改,移除给定类型的一层指针,std::remove_pointer)

类型特性

类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。

试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。

定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。

类型修改

类型修改模板通过应用修改到模板参数,创建新类型定义。结果类型可以通过成员 typedef type 访问。

移除给定类型的一层指针

std::remove_pointer

|----------------------------------------------|---|-----------|
| template< class T > struct remove_pointer; | | (C++11 起) |

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

成员类型

|--------|------------------------------|
| 名称 | 定义 |
| 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 <iostream>
#include <type_traits>

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_same<int, int>():                 "
              << std::is_same<int, int>() << std::endl;
    std::cout << "std::is_same<int, int*>():                "
              << std::is_same<int, int*>() << std::endl;
    std::cout << "std::is_same<int, int**>():               "
              << std::is_same<int, int**>() << std::endl;
    std::cout << "----------------------------------------------------------------------" << std::endl;

    std::cout << "std::is_same<int, std::remove_pointer<int>::type>():      "
              << std::is_same<int, std::remove_pointer<int>::type>() << std::endl;
    std::cout << "std::is_same<int, std::remove_pointer<int*>::type>():     "
              << std::is_same<int, std::remove_pointer<int*>::type>() << std::endl;
    std::cout << "std::is_same<int, std::remove_pointer<int**>::type>():    "
              << std::is_same<int, std::remove_pointer<int**>::type>() << std::endl;
    std::cout << "----------------------------------------------------------------------" << std::endl;

    std::cout << "std::is_same<int, std::remove_pointer<int* const>::type>():           "
              << std::is_same<int, std::remove_pointer<int* const>::type>() << std::endl;
    std::cout << "std::is_same<int, std::remove_pointer<int* volatile>::type>():        "
              << std::is_same<int, std::remove_pointer<int* volatile>::type>() << std::endl;
    std::cout << "std::is_same<int, std::remove_pointer<int* const volatile>::type>():  "
              << std::is_same<int, std::remove_pointer<int* const volatile>::type>() << std::endl;

    return 0;
}

输出

std::is_same<int, int>():                 true
std::is_same<int, int*>():                false
std::is_same<int, int**>():               false
----------------------------------------------------------------------
std::is_same<int, std::remove_pointer<int>::type>():      true
std::is_same<int, std::remove_pointer<int*>::type>():     true
std::is_same<int, std::remove_pointer<int**>::type>():    false
----------------------------------------------------------------------
std::is_same<int, std::remove_pointer<int* const>::type>():           true
std::is_same<int, std::remove_pointer<int* volatile>::type>():        true
std::is_same<int, std::remove_pointer<int* const volatile>::type>():  true
相关推荐
9毫米的幻想2 分钟前
【Linux系统】—— 冯诺依曼体系结构与操作系统初理解
linux·运维·服务器·c语言·c++
Mr.Wang80922 分钟前
条款23:宁以non-member、non-friend替换member函数
开发语言·c++
以卿a2 小时前
C++ 模板初阶
开发语言·c++
黑不溜秋的7 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
Dream it possible!9 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴9 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程9 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空11 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc77412 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白112 小时前
C++——list模拟实现
开发语言·c++