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 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]
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>
불완전성과 관계없이 동작이 정의됨