<type_traits>

類模板
<type_traits>

std::add_cv

template <class T> struct add_cv;
新增 const volatile 限定符
獲取型別T同時具有constvolatile的頂層限定符。

轉換後的類型別名為成員型別add_cv::type.

如果已知T如果 T 尚未被 const-volatile- 限定,並且不是引用或函式(它們不能被 cv 限定),則它與T但是constvolatile已限定。否則,它是T不變。

請注意,此類僅使用另一個型別作為模型來獲取型別,但它不轉換這些型別之間的值或物件。要顯式為物件新增 cv 限定,請使用const_cast可以使用。

模板引數

T
一個型別。

成員型別

成員型別定義
型別如果已知T如果 T 尚未被 const-volatile-限定,並且不是引用或函式,則與T但已新增 cv 限定。
否則,T

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// add_cv example
#include <iostream>
#include <type_traits>

int main() {
  typedef std::add_cv<int>::type A;
  typedef std::add_cv<const int>::type B;
  typedef std::add_cv<volatile int>::type C;
  typedef std::add_cv<const volatile int>::type D;

  std::cout << std::boolalpha;
  std::cout << "typedefs of const volatile int:" << std::endl;
  std::cout << "A: " << std::is_same<const volatile int,A>::value << std::endl;
  std::cout << "B: " << std::is_same<const volatile int,B>::value << std::endl;
  std::cout << "C: " << std::is_same<const volatile int,C>::value << std::endl;
  std::cout << "D: " << std::is_same<const volatile int,D>::value << std::endl;

  return 0;
}

輸出
typedefs of const volatile int:
A: true
B: true
C: true
D: true


另見