is_aggregate (집계 타입 판별)
std::is_aggregate는 주어진 타입이 C++ 표준에서 정의하는 집계(aggregate) 타입인지 컴파일 타임에 판별하는 타입 특성(type trait)이에요. C++17부터 사용할 수 있으며, 집계 타입이면 value가 true가 되고, 그 외의 타입이면 false가 돼요. 이 특성은 특히 제네릭 코드에서 객체를 초기화하는 방식을 결정할 때 유용하게 쓰여요.
출처: cppreference
본문
정의
<type_traits> 헤더에 정의되어 있어요.
| 정의 |
|
|
template < class T > struct is_aggregate ; |
|
(C++17부터) |
std::is_aggregate는 UnaryTypeTrait이에요.
T가 집계 타입이면 멤버 상수 value가 true가 돼요. 그 외의 모든 타입에 대해서는 value가 false예요.
T가 배열 타입이 아니거나 (cv 한정된) void가 아닌 불완전한 타입이라면, 동작이 정의되지 않아요.
프로그램이 std::is_aggregate 또는 std::is_aggregate_v에 대한 특수화를 추가하면, 동작이 정의되지 않아요.
템플릿 매개변수
도우미 변수 템플릿
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를 반환해요 (공용 멤버 함수) |
operator() (C++14) |
value를 반환해요 (공용 멤버 함수) |
멤버 타입
| 타입 |
정의 |
value_type |
bool |
type |
std :: integral_constant < bool , value > |
참고 사항
| 기능 테스트 매크로 |
값 |
표준 |
기능 |
__cpp_lib_is_aggregate |
201703L |
(C++17) |
std::is_aggregate |
예제
#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>의 불완전성과 관계없이 동작이 정의돼요. |
같이 보기
is_aggregate_type (C++26) |
반영된 타입이 집계 타입인지 확인해요 (함수) [편집] |
더 알아보기 (Learn more)
cppreference