Namespaces
Variants

std:: is_aggregate

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
is_aggregate
(C++17)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定義先ヘッダ <type_traits>
template < class T >
struct is_aggregate ;
(C++17以降)

std::is_aggregate UnaryTypeTrait です。

T 集成体型 である場合、メンバ定数 value true に設定します。それ以外の型の場合、 value false となります。

T が配列型または(possibly cv-qualified) void 以外の不完全型である場合、動作は未定義です。

プログラムが std::is_aggregate または std::is_aggregate_v に対する特殊化を追加する場合、動作は未定義です。

目次

翻訳のポイント: - 「Contents」→「目次」に翻訳 - C++関連の専門用語(Template parameters、Helper variable template、std::integral_constant、Member constants、Member functions、Member types、Notes、Example、Defect reports)は原文のまま保持 - HTMLタグ、属性、数値は一切変更せず - フォーマットと構造を完全に維持

テンプレートパラメータ

T - チェックする型

ヘルパー変数テンプレート

template < class T >
constexpr bool is_aggregate_v = is_aggregate < T > :: value ;
(C++17以降)

std:: integral_constant から継承

メンバ定数

value
[static]
true T が集成体型の場合)、 false (それ以外の場合)
(public static member constant)

メンバ関数

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>
// 初期化されていないメモリ位置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; };
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> の不完全性に関わらず
動作は定義される