<type_traits>

類模板
<type_traits>

std::remove_cv

template <class T> struct remove_cv;
移除 cv 限定
獲取型別T除去頂層的constvolatile限定符。

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

如果已知T如果型別是cv限定(即是const和/或volatile),那麼它和T但移除了其cv限定。否則,它就是T不變。

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

模板引數

T
一個型別。

成員型別

成員型別定義
型別如果已知T如果型別是cv限定,則與T相同,但移除了任何 cv 限定。
否則,T

示例

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

int main() {
  typedef const volatile char cvchar;
  std::remove_cv<cvchar>::type a;       // char a
  std::remove_cv<char* const>::type b;  // char* b
  std::remove_cv<const char*>::type c;  // const char* c (no changes)

  if (std::is_const<decltype(a)>::value)
    std::cout << "type of a is const" << std::endl;
  else
    std::cout << "type of a is not const" << std::endl;

  if (std::is_volatile<decltype(a)>::value)
    std::cout << "type of a is volatile" << std::endl;
  else
    std::cout << "type of a is not volatile" << std::endl;

  return 0;
}

輸出
type of a is not const
type of a is not volatile


另見