std::is_aggregate
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct is_aggregate; |
(C++17以降) | |
std::is_aggregate は 単項型特性 (UnaryTypeTrait) です。
T が 集約型 (aggregate type) である場合、メンバー定数 value を true に設定して提供します。それ以外の型の場合、value は false となります。
T が配列型以外の不完全型、または (おそらく cv 修飾された) void である場合、動作は未定義です。
プログラムが std::is_aggregate または std::is_aggregate_v の特殊化を追加した場合、動作は未定義です。
目次 |
[編集] テンプレートパラメータ
| T | - | チェックする型 |
[編集] ヘルパー変数テンプレート
| template< class T > constexpr bool is_aggregate_v = is_aggregate<T>::value; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
T が集約型であれば true、そうでなければ false(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[編集] 備考
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_is_aggregate |
201703L |
(C++17) | std::is_agregate
|
[編集] 例
このコードを実行
#include <algorithm> #include <cassert> #include <cstddef> #include <new> #include <string_view> #include <type_traits> #include <utility> // Constructs a T at the uninitialized memory pointed to by p using // list-initialization for aggregates and non-list initialization otherwise. 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; }; static_assert(std::is_aggregate_v<A>); struct B { int i; std::string_view str; B(int i, std::string_view str) : i(i), str(str) {} }; static_assert(not std::is_aggregate_v<B>); template <typename... Ts> using aligned_storage_t = alignas(Ts...) std::byte[std::max({sizeof(Ts)...})]; int main() { aligned_storage_t<A, B> storage; A& a = *construct(reinterpret_cast<A*>(&storage), 1, 2); assert(a.x == 1 and a.y == 2); B& b = *construct(reinterpret_cast<B*>(&storage), 3, "4"); assert(b.i == 3 and b.str == "4"); }
[編集] 欠陥報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3823 | C++17 | T が配列型であってもstd::remove_all_extents_t<T> が不完全型である場合、動作は未定義です。 |
T が配列型である限り、std::remove_all_extents_t<T> の不完全性にかかわらず動作は定義されます。 |