<type_traits>

類模板
<type_traits>

std::add_volatile

template <class T> struct add_volatile;
新增 volatile 限定符
獲取型別Tvolatile的頂層限定符。

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

如果已知T如果 T 尚未被 volatile 限定,並且既不是引用型別也不是函式型別(函式型別不能被 volatile 限定),則其型別與T volatile相同。否則,其型別為T不變。

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

模板引數

T
一個型別。

成員型別

成員型別定義
型別如果已知T如果 T 尚未被 volatile 限定,並且既不是引用型別也不是函式型別,則其型別與T相同,但被 volatile 限定。
否則,T

示例

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

int main() {
  typedef std::add_volatile<int>::type A;            // volatile int
  typedef std::add_volatile<volatile int>::type B;   // volatile int     (unchanged)
  typedef std::add_volatile<int* volatile>::type C;  // int* volatile    (unchanged)
  typedef std::add_volatile<volatile int*>::type D;  // volatile int* volatile
  typedef std::add_volatile<volatile int&>::type E;  // volatile int&    (unchanged)

  std::cout << std::boolalpha;
  std::cout << "checking volatileness:" << std::endl;
  std::cout << "A: " << std::is_volatile<A>::value << std::endl;
  std::cout << "B: " << std::is_volatile<B>::value << std::endl;
  std::cout << "C: " << std::is_volatile<C>::value << std::endl;
  std::cout << "D: " << std::is_volatile<D>::value << std::endl;
  std::cout << "E: " << std::is_volatile<E>::value << std::endl;

  return 0;
}

輸出
checking volatileness:
A: true
B: true
C: true
D: true
E: false


另見