当前位置: 首页 > news >正文

C++标准模板(STL)- 类型支持 (类型修改,从给定类型移除 const 或/与 volatile 限定符,std::remove_cv)

类型特性


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

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

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

类型修改

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

从给定类型移除 const 或/与 volatile 限定符

std::remove_cv, 
std::remove_const, 
std::remove_volatile

template< class T >
struct remove_cv;

(1)(C++11 起)

template< class T >
struct remove_const;

(2)(C++11 起)

template< class T >
struct remove_volatile;

(3)(C++11 起)

提供与 T 相同的成员 typedef type ,除了其最顶层 cv 限定符被移除。

1) 移除最顶层 const 、最顶层 volatile 或两者,若存在。

2) 移除最顶层 const

3) 移除最顶层 volatile

成员类型

名称定义
type无 cv 限定符的 T

辅助类型

template< class T >
using remove_cv_t       = typename remove_cv<T>::type;

(C++14 起)

template< class T >
using remove_const_t    = typename remove_const<T>::type;

(C++14 起)

template< class T >
using remove_volatile_t = typename remove_volatile<T>::type;

(C++14 起)

 可能的实现

template< class T >
struct remove_cv {typedef typename std::remove_volatile<typename std::remove_const<T>::type>::type type;
};template< class T > struct remove_const          { typedef T type; };
template< class T > struct remove_const<const T> { typedef T type; };template< class T > struct remove_volatile             { typedef T type; };
template< class T > struct remove_volatile<volatile T> { typedef T type; };

调用示例

#include <iostream>
#include <type_traits>int main()
{typedef std::remove_cv<const int>::type CVtype1;typedef std::remove_cv<volatile int>::type CVtype2;typedef std::remove_cv<const volatile int>::type CVtype3;typedef std::remove_cv<const volatile int*>::type CVtype4;typedef std::remove_cv<int * const volatile>::type CVtype5;std::cout << "std::is_same<int, std::remove_cv<const int>::type>::value:    "<< (std::is_same<int, CVtype1>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int, std::remove_cv<volatile int>::type>::value: "<< (std::is_same<int, CVtype2>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int, std::remove_cv<const volatile int>::type>::value:"<< (std::is_same<int, CVtype3>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<const volatile int*, std::remove_cv<const volatile int*>::type>::value:  "<< (std::is_same<const volatile int*, CVtype4>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int*, std::remove_cv<int * const volatile>::type>::value: "<< (std::is_same<int*, CVtype5>::value ? "passed" : "failed") << std::endl;std::cout << std::endl;typedef std::remove_const<const int>::type Ctype1;typedef std::remove_const<volatile int>::type Ctype2;typedef std::remove_const<const volatile int>::type Ctype3;typedef std::remove_const<const volatile int*>::type Ctype4;typedef std::remove_const<int * const volatile>::type Ctype5;std::cout << "std::is_same<int, std::remove_const<const int>::type>::value:    "<< (std::is_same<int, Ctype1>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int, std::remove_const<volatile int>::type>::value: "<< (std::is_same<int, Ctype2>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int, std::remove_const<const volatile int>::type>::value:"<< (std::is_same<int, Ctype3>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<const volatile int*, std::remove_const<const volatile int*>::type>::value:  "<< (std::is_same<const volatile int*, Ctype4>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int*, std::remove_const<int * const volatile>::type>::value: "<< (std::is_same<int*, Ctype5>::value ? "passed" : "failed") << std::endl;std::cout << std::endl;typedef std::remove_volatile<const int>::type Vtype1;typedef std::remove_volatile<volatile int>::type Vtype2;typedef std::remove_volatile<const volatile int>::type Vtype3;typedef std::remove_volatile<const volatile int*>::type Vtype4;typedef std::remove_volatile<int * const volatile>::type Vtype5;std::cout << "std::is_same<int, std::remove_volatile<const int>::type>::value:    "<< (std::is_same<int, Vtype1>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int, std::remove_volatile<volatile int>::type>::value: "<< (std::is_same<int, Vtype2>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int, std::remove_volatile<const volatile int>::type>::value:"<< (std::is_same<int, Vtype3>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<const volatile int*, std::remove_volatile<const volatile int*>::type>::value:  "<< (std::is_same<const volatile int*, Vtype4>::value ? "passed" : "failed") << std::endl;std::cout << "std::is_same<int*, std::remove_volatile<int * const volatile>::type>::value: "<< (std::is_same<int*, Vtype5>::value ? "passed" : "failed") << std::endl;std::cout << std::endl;return 0;
}

输出

std::is_same<int, std::remove_cv<const int>::type>::value:    passed
std::is_same<int, std::remove_cv<volatile int>::type>::value: passed
std::is_same<int, std::remove_cv<const volatile int>::type>::value:passed
std::is_same<const volatile int*, std::remove_cv<const volatile int*>::type>::value:  passed
std::is_same<int*, std::remove_cv<int * const volatile>::type>::value: passedstd::is_same<int, std::remove_const<const int>::type>::value:    passed
std::is_same<int, std::remove_const<volatile int>::type>::value: failed
std::is_same<int, std::remove_const<const volatile int>::type>::value:failed
std::is_same<const volatile int*, std::remove_const<const volatile int*>::type>::value:  passed
std::is_same<int*, std::remove_const<int * const volatile>::type>::value: failedstd::is_same<int, std::remove_volatile<const int>::type>::value:    failed
std::is_same<int, std::remove_volatile<volatile int>::type>::value: passed
std::is_same<int, std::remove_volatile<const volatile int>::type>::value:failed
std::is_same<const volatile int*, std::remove_volatile<const volatile int*>::type>::value:  passed
std::is_same<int*, std::remove_volatile<int * const volatile>::type>::value: failed

http://www.lryc.cn/news/238997.html

相关文章:

  • nodejs搭建本地服务
  • 如何看待Unity新收费模式?
  • Excel数据可视化—波士顿矩阵图【四象限图】
  • 【Java】智慧工地管理系统源代码,支持二次开发,SaaS模式
  • Lstm+transformer的刀具磨损预测
  • 本机idea连接虚拟机中的Hbase
  • .NET中的Object类学习3_MemberwiseClone方法
  • 鼎捷前端开发校招岗技术面面经(已过)
  • Rockchip平台rk3588源码下载编译(基于Android13)
  • RuntimeError: PyPI no longer supports ‘pip search‘ (or XML-RPC search).
  • 21款奔驰GLS450升级23P驾驶辅助 提升安全出行
  • iOS越狱检测总结
  • 场景驱动的 AI 体验设计:如何让智能 IDE 赋能遗留系统重写
  • 【封装UI组件库系列】搭建项目及准备工作
  • C#使用DateTime获取日期和时间
  • rook-ceph部署
  • JVM基础- 垃圾回收器
  • 数理统计的基本概念(二)
  • CountDownLatch和CyclicBarrier
  • 云原生正在重塑软件的整个生命周期(内附资料)
  • Node.js环境配置级安装vue-cli脚手架
  • 十七、Rust集成MQTT Client
  • HarmonyOS ArkTS开发语言介绍(三)
  • [架构之路-247]:目标系统 - 设计方法 - 软件工程 - 结构化方法的基本思想、本质、特点以及在软件开发、在生活中的应用
  • 大模型的交互能力
  • 80%测试员被骗,关于jmeter 的一个弥天大谎!
  • Git——感谢尚硅谷官方文档
  • Java WebSocket框架
  • C#实现本地服务器客户端私聊通信
  • PyTorch 之 Dataset 类入门学习