当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::remove_volatile用法及代码示例


<type_traits>头文件中提供了C++ STL的std::remove_volatile模板。 C++ STL的std::remove_volatile模板用于获取T,而不使用volatile限定。如果T不具有volatile限定,则它返回布尔值true,否则返回false。以下是相同的语法:

头文件:

#include<type_traits>

模板类别:

template <class <T>
struct remove_volatile;

用法:

std::remove_volatile::value

参数:模板std::rmeove_volatile接受单个参数T(Trait类),以检查T是否不具有volatile限定。



返回值:模板std::remove_volatile返回一个布尔值:

  • True:如果类型T没有挥发合格。
  • False:如果类型T具有挥发性认证。

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

程序1:

// C++ program to illustrate 
// std::remove_volatile 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Driver Code 
int main() 
{ 
  
    // Declare variable of type 
    // volatile (int, const int, const int*, 
    // int * const and const int&) 
    typedef remove_volatile<int>::type A; 
    typedef remove_volatile<volatile int>::type B; 
    typedef remove_volatile<int* volatile>::type C; 
    typedef remove_volatile<volatile int*>::type D; 
    typedef remove_volatile<volatile int&>::type E; 
  
    cout << boolalpha; 
  
    // Checking the non-volatileness 
    // of the above declared variable 
    cout << "checking Non-volatileness:"
         << endl; 
  
    cout << "A:"
         << is_volatile<A>::value 
         << endl; 
  
    cout << "B:"
         << is_volatile<B>::value 
         << endl; 
  
    cout << "C:"
         << is_volatile<C>::value 
         << endl; 
  
    cout << "D:"
         << is_volatile<D>::value 
         << endl; 
  
    cout << "E:"
         << is_volatile<E>::value 
         << endl; 
  
    return 0; 
}
输出:
checking Non-volatileness:
A:false
B:false
C:false
D:false
E:false

程序2:

// C++ program to illustrate 
// std::remove_volatile 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Driver Code 
int main() 
{ 
  
    // Declare variable of type 
    // volatile (int, const int, const int*, 
    // int * const and const int&) 
    typedef add_volatile<int>::type A; 
    typedef add_volatile<volatile int>::type B; 
    typedef remove_cv<float>::type a; 
    typedef remove_cv<char* const>::type b; 
    typedef remove_cv<const char*>::type c; 
  
    cout << boolalpha; 
  
    // Checking the non-volatileness 
    // of the above declared variable 
    cout << "checking Non-volatileness:"
         << endl; 
  
    cout << "A:"
         << is_volatile<A>::value 
         << endl; 
  
    cout << "B:"
         << is_volatile<B>::value 
         << endl; 
  
    cout << "C:"
         << is_volatile<a>::value 
         << endl; 
  
    cout << "D:"
         << is_volatile<b>::value 
         << endl; 
  
    cout << "E:"
         << is_volatile<c>::value 
         << endl; 
  
    return 0; 
}
输出:
checking Non-volatileness:
A:true
B:true
C:false
D:false
E:false

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




相关用法


注:本文由纯净天空筛选整理自bansal_rtk_大神的英文原创作品 std::remove_volatile in C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。