std:: is_const
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct is_const ; |
(C++11 起) | |
std::is_const
是一个
UnaryTypeTrait
。
如果
T
是 const 限定类型(即
const
,或
const
volatile
),则提供等于
true
的成员常量
value
。对于任何其他类型,
value
为
false
。
如果程序为
std::is_const
或
std::is_const_v
添加特化,则行为是未定义的。
目录 |
模板参数
| T | - | 待检查的类型 |
辅助变量模板
|
template
<
class
T
>
constexpr bool is_const_v = is_const < T > :: value ; |
(C++17 起) | |
继承自 std:: integral_constant
成员常量
|
value
[static]
|
若
T
为 const 限定类型则为
true
,否则为
false
(公开静态成员常量) |
成员函数
|
operator bool
|
转换对象为
bool
类型,返回
value
(公开成员函数) |
|
operator()
(C++14)
|
返回
value
(公开成员函数) |
成员类型
| 类型 | 定义 |
value_type
|
bool |
type
|
std:: integral_constant < bool , value > |
注释
如果 T 是引用类型,那么 is_const < T > :: value 始终为 false 。检查可能为引用类型的常量正确性的正确方式是移除引用: is_const < typename remove_reference < T > :: type > 。
可能的实现
template<class T> struct is_const : std::false_type {}; template<class T> struct is_const<const T> : std::true_type {}; |
示例
#include <type_traits> static_assert(std::is_same_v<const int*, int const*>, "请记住,常量性在指针内部紧密绑定。"); static_assert(!std::is_const_v<int>); static_assert(std::is_const_v<const int>); static_assert(!std::is_const_v<int*>); static_assert(std::is_const_v<int* const>, "因为指针本身不可更改,但指向的整型值可以更改。"); static_assert(!std::is_const_v<const int*>, "因为指针本身可以更改,但指向的整型值不可更改。"); static_assert(!std::is_const_v<const int&>); static_assert(std::is_const_v<std::remove_reference_t<const int&>>); struct S { void foo() const {} void bar() const {} }; int main() { // 常量成员函数的常量性表现方式不同: static_assert(!std::is_const_v<decltype(&S::foo)>, "因为 &S::foo 是一个指针。"); using S_mem_fun_ptr = void(S::*)() const; S_mem_fun_ptr sfp = &S::foo; sfp = &S::bar; // 正常,可以重新指向 static_assert(!std::is_const_v<decltype(sfp)>, "因为 sfp 是相同的指针类型,因此可以重新指向。"); const S_mem_fun_ptr csfp = &S::foo; // csfp = &S::bar; // 错误 static_assert(std::is_const_v<decltype(csfp)>, "因为 csfp 不能重新指向。"); }
参见
|
(C++11)
|
检查类型是否具有 volatile 限定符
(类模板) |
|
(C++17)
|
获取指向其参数的
const
引用
(函数模板) |