名前空間
変種
操作

std::is_aggregate

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型のプロパティ
(C++11)
(C++11)
(C++14)
is_aggregate
(C++17)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
型の変更
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
 
ヘッダ <type_traits> で定義
template< class T >
struct is_aggregate;
(C++17以降)

std::is_aggregate単項型特性 (UnaryTypeTrait) です。

T集約型 (aggregate type) である場合、メンバー定数 valuetrue に設定して提供します。それ以外の型の場合、valuefalse となります。

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> の不完全性にかかわらず
動作は定義されます。
English 日本語 中文(简体) 中文(繁體)