std::remove_cv, std::remove_const, std::remove_volatile

来自cppreference.com
< cpp‎ | types

 
 
 
类型支持
基本类型
基础类型
定宽整数类型 (C++11)
数值极限
C 数值极限接口
运行时类型信息
类型特性
类型类别
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++17中过时)
(C++11)
类型特性常量
元函数
(C++17)
受支持操作
关系与属性查询
(C++11)
(C++11)
(C++11)
(C++11)
类型修改
remove_cvremove_constremove_volatile
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型变换
(C++11)
(C++11)
(C++17)
(C++11)(C++17中过时)
(C++17)
 
定义于头文件 <type_traits>
template< class T >
struct remove_cv;
(1) (C++11 起)
template< class T >
struct remove_const;
(2) (C++11 起)
template< class T >
struct remove_volatile;
(3) (C++11 起)

Provides the member typedef type which is the same as T, except that its topmost cv-qualifiers are removed.

1) removes the topmost const, the topmost volatile, or both, if present.

2) removes the topmost const

3) removes the topmost volatile

目录

[编辑] 会员类型

姓名
原文:
Name
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
Definition
type the type T without cv-qualifier

[编辑] 可能的实现

template< class T >
struct remove_cv {
    typedef typename std::remove_volatile<typename std::remove_const<T>::type>::type type;
};
 
template< class T > struct remove_const          { typedef T type; };
template< class T > struct remove_const<const T> { typedef T type; };
 
template< class T > struct remove_volatile             { typedef T type; };
template< class T > struct remove_volatile<volatile T> { typedef T type; };

[编辑] 示例

Removing const/volatile from const volatile int * does not modify the type, because the pointer itself is neither const nor volatile.

#include <iostream>
#include <type_traits>
 
int main() {
    typedef std::remove_cv<const int>::type type1;
    typedef std::remove_cv<volatile int>::type type2;
    typedef std::remove_cv<const volatile int>::type type3;
    typedef std::remove_cv<const volatile int*>::type type4;
    typedef std::remove_cv<int * const volatile>::type type5;
 
    std::cout << "test1 " << (std::is_same<int, type1>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test2 " << (std::is_same<int, type2>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test3 " << (std::is_same<int, type3>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test4 " << (std::is_same<const volatile int*, type4>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test5 " << (std::is_same<int*, type5>::value
        ? "passed" : "failed") << '\n';
}

输出:

test1 passed
test2 passed
test3 passed
test4 passed
test5 passed

[编辑] 另请参阅

(C++11)
检查类型是否包含const修饰符
(类模板) [编辑]
检查类型是否包含volatile修饰符
(类模板) [编辑]