#include #include #include // 于 p 所指向的未初始化内存构造 T // 用聚合体的列表初始化,否则使用非列表初始化 template T* construct(T* p, Args&&... args) { if constexpr(std::is_aggregate_v) { return ::new (static_cast(p)) T{std::forward(args)...}; } else { return ::new (static_cast(p)) T(std::forward(args)...); } } struct A { int x, y; }; struct B { B(int, const char*) { } }; int main() { std::aligned_union_t<1, A, B> storage; A* a = construct(reinterpret_cast(&storage), 1, 2); B* b = construct(reinterpret_cast(&storage), 1, "hello"); }