std::is_aggregate
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <type_traits>
  | 
||
|   template< class T > struct is_aggregate;  | 
(C++17 起) | |
检查 T 是否为聚合类型。若 T 为聚合类型,则成员常量 value 等于 true ,否则为 false 。
若 std::remove_all_extents_t<T> 是异于(可以有 cv 限定的) void 的不完整类型,则行为未定义。
添加 is_aggregate 或 is_aggregate_v 的特化的程序行为未定义。
模板形参
| T | - | 要检查的类型 | 
辅助变量模板
|   template< class T > inline constexpr bool is_aggregate_v = is_aggregate<T>::value;  | 
(C++17 起) | |
继承自 std::integral_constant
成员常量
|    value [静态]  | 
   若 T为聚合类型则为 true ,否则为 false  (公开静态成员常量)  | 
成员函数
|    operator bool  | 
   转换对象为 bool ,返回 value  (公开成员函数)  | 
|    operator() (C++14)  | 
   返回 value  (公开成员函数)  | 
成员类型
| 类型 | 定义 | 
  value_type
 | 
  bool
 | 
  type
 | 
std::integral_constant<bool, value> | 
示例
运行此代码
#include <type_traits> #include <new> #include <utility> // 于 p 所指向的未初始化内存构造 T // 用聚合体的列表初始化,否则使用非列表初始化 template<class T, class... Args> T* construct(T* p, Args&&... args) { if constexpr(std::is_aggregate_v<T>) { return ::new (static_cast<void*>(p)) T{std::forward<Args>(args)...}; } else { return ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...); } } struct A { int x, y; }; struct B { B(int, const char*) { } }; int main() { std::aligned_union_t<1, A, B> storage; A* a = construct(reinterpret_cast<A*>(&storage), 1, 2); B* b = construct(reinterpret_cast<B*>(&storage), 1, "hello"); }