C++中的std::remove_cv用法示例介绍

2021年4月24日13:07:10 发表评论 1,436 次浏览

std::remove_cv的模板C++ STL在<type_traits>头文件中。C++ STL模板的std::remove_cv用于获取类型T没有const和易挥发的资格。如果满足以下条件, 则返回布尔值true:T没有const和volatile限定, 否则返回false。

头文件:

#include<type_traits>

模板:

template<class T>
struct remove_cv;

语法如下:

std::remove_cv<T>::type a;

参数:模板std::remove_cv接受一个参数T(特质班)检查是否T没有const和volatile是否合格。

返回值:

下面是演示程序std::remove_cv在C++中:

程序:

//C++ program to illustrate std::remove_cv
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
//Driver Code
int main()
{
  
     //Declare variable of type int, const
     //int, volatile int and const volatile int
     typedef remove_cv<int>::type A;
     typedef remove_cv<const int>::type B;
     typedef remove_cv<volatile int>::type C;
     typedef remove_cv<const volatile int &>::type D;
  
     cout <<boolalpha;
  
     cout <<"A: "
          <<is_same<const volatile int , A>::value
          <<endl;
  
     cout <<"B: "
          <<is_same<const volatile int , B>::value
          <<endl;
  
     cout <<"C: "
          <<is_same<int , C>::value
          <<endl;
  
     cout <<"D: "
          <<is_same<int , D>::value
          <<endl;
  
     return 0;
}

输出如下:

A: false
B: false
C: true
D: false

参考: http://www.cplusplus.com/reference/type_traits/remove_cv/

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C++ STL通过激烈的问题解决过程来训练和掌握这些概念。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: