std::is_structural
来自cppreference.com
| 在标头 <type_traits> 定义
|
||
| |
(C++26 起) | |
std::is_structural 是一元类型特征 (UnaryTypeTrait) 。
如果 T 是结构式类型,则提供的成员常量 value 等于 true。对于任何其他类型,value 为 false。
如果程序添加了 std::is_structural 或 std::is_structural_v 的特化,那么行为未定义。
模板形参
| T | - | 要检查的类型 |
辅助变量模板
| |
(C++26 起) | |
继承自 std::integral_constant
成员常量
value [静态] |
如果 T 是结构式类型那么是 true,否则是 false (公开静态成员常量) |
成员函数
operator bool |
将对象转换到 bool,返回 value (公开成员函数) |
operator() (C++14) |
返回 value (公开成员函数) |
成员类型
| 类型 | 定义 |
value_type
|
bool
|
type
|
std::integral_constant<bool, value>
|
注解
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_is_structural |
202603L |
(C++26) | std::is_structural
|
示例
Run this code
#include <type_traits>
struct S { int x; };
class Bad1 { int x; };
struct Bad2 { mutable int x; };
struct A { int a[2][3]; };
struct B : public S {}; // OK:结构式类型为公开基类
struct C : private S {}; // 非结构式:私有基类
auto L = [](int n){ return -n; }; // 无捕获的 lambda
static_assert( std::is_structural_v<int>); // 标量
static_assert( std::is_structural_v<int&>); // 左值引用
static_assert(!std::is_structural_v<int&&>); // 右值引用
static_assert( std::is_structural_v<S>);
static_assert(!std::is_structural_v<Bad1>);
static_assert(!std::is_structural_v<Bad2>);
static_assert( std::is_structural_v<A>);
static_assert( std::is_structural_v<B>);
static_assert(!std::is_structural_v<C>);
static_assert( std::is_structural_v<decltype(L)>);
int main() { }
参阅
(C++26) |
检查反射是否表示结构式类型 (函数) |