std::is_aggregate(3)
π NAME π
okay so bestie, this is std::is_aggregate β the whole NAME section is literally std::is_aggregate - std::is_aggregate, and that's iconic. We're in the C++ Standard Libary manual, page std::is_aggregate(3), and yes I'm obsessed. It's giving "we check if a type is an aggregate" energy. periodt. π
β¨ Synopsis β¨
Defined in header <type_traits> β exact, don't forget it:
Defined in header <type_traits>
template< class T > (since C++17)
struct is_aggregate;
std::is_aggregate is a UnaryTypeTrait. No cap.
If T is an aggregate type, it provides the member constant value equal true. For any other type, value is false. Slay.
β οΈ ifTis an incomplete type other than an array type or (possibly cv-qualified)void, the behavior is undefined. That's UB, bestie β not the cute kind. π
π¨ if the program adds specializations forstd::is_aggregateorstd::is_aggregate_v, the behavior is undefined. Do not do that. Not today. Not ever. π
πΈ Template parameters πΈ
Tβ a type to check. That's the whole job, lowkey.
π Helper variable template π
template< class T >
(since C++17)
inline constexpr bool is_aggregate_v = is_aggregate<T>::value;
π« Inherited from std::integral_constant π«
π Member constants π
valueβtrueifTis an aggregate type,falseotherwise.[static](public static member constant).
π¦ Member functions π¦
operator boolβ converts the object tobool, returnsvalue(public member function).operator()β returnsvalue(C++14) (public member function).
π Member types π
value_typeβbooltypeβstd::integral_constant<bool, value>
π Notes π
Feature-test macro situation, iykyk:
- Feature-test macro:
__cpp_lib_is_aggregateβ Value:201703Lβ Std:(C++17)β Feature:std::is_agregate
π₯ Example π₯
// Run this code β go off, bestie:
#include <new>
#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; };
struct B { B(int, const char*) {} };
int main()
{
std::aligned_union_t<1, A, B> storage;
[[maybe_unused]] A* a = construct(reinterpret_cast<A*>(&storage), 1, 2);
[[maybe_unused]] B* b = construct(reinterpret_cast<B*>(&storage), 1, "hello");
}
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History lesson, but make it fashion. β¨
- DR
LWG 3823, applied to C++17 β Behavior as published: "The behavior is undefined ifTis an array type butstd::remove_all_extents_t<T>is an incomplete type." Correct behavior: "The behavior is defined regardless of the incompleteness ofstd::remove_all_extents_t<T>as long asTis an array type."
okay bye bestie π«Ά this came from source http://cppreference.com, version/date 2024.06.10, and it's the std::is_aggregate(3) page signing off. mwah ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms