std:: add_cv, std:: add_const, std:: add_volatile
From cppreference.net
C++
Metaprogramming library
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct add_cv ; |
(1) | (C++11 起) |
|
template
<
class
T
>
struct add_const ; |
(2) | (C++11 起) |
|
template
<
class
T
>
struct add_volatile ; |
(3) | (C++11 起) |
提供成员类型定义
type
,该类型与
T
相同,但会添加 cv 限定符(除非
T
是函数、引用类型或已具有该 cv 限定符)
1)
同时添加
const
和
volatile
2)
添加
const
3)
添加
volatile
如果程序对本页面描述的任何模板添加特化,则行为未定义。
目录 |
成员类型
| 名称 | 定义 |
type
|
带有 cv 限定符的类型
T
|
辅助类型
|
template
<
class
T
>
using add_cv_t = typename add_cv < T > :: type ; |
(C++14 起) | |
|
template
<
class
T
>
using add_const_t = typename add_const < T > :: type ; |
(C++14 起) | |
|
template
<
class
T
>
using add_volatile_t = typename add_volatile < T > :: type ; |
(C++14 起) | |
可能的实现
template<class T> struct add_cv { typedef const volatile T type; }; template<class T> struct add_const { typedef const T type; }; template<class T> struct add_volatile { typedef volatile T type; }; |
标签内的C++代码均未翻译,仅保留原始格式。由于页面中除代码外无其他需翻译的文本内容,故输出保持原样。)
注释
这些转换特征可用于在模板实参推导中建立 非推导上下文 :
template<class T> void f(const T&, const T&); template<class T> void g(const T&, std::add_const_t<T>&); f(4.2, 0); // 错误:为 'T' 推导出冲突的类型 g(4.2, 0); // 正确:调用 g<double>
示例
运行此代码
#include <iostream> #include <type_traits> struct foo { void m() { std::cout << "Non-cv\n"; } void m() const { std::cout << "Const\n"; } void m() volatile { std::cout << "Volatile\n"; } void m() const volatile { std::cout << "Const-volatile\n"; } }; int main() { foo{}.m(); std::add_const<foo>::type{}.m(); std::add_volatile<foo>::type{}.m(); std::add_cv<foo>::type{}.m(); }
输出:
Non-cv Const Volatile Const-volatile
参见
|
(C++11)
|
检查类型是否具有 const 限定符
(类模板) |
|
(C++11)
|
检查类型是否具有 volatile 限定符
(类模板) |
|
(C++11)
(C++11)
(C++11)
|
从给定类型移除
const
和/或
volatile
限定符
(类模板) |
|
(C++17)
|
获取指向其参数的
const
引用
(函数模板) |