Files
clang-p2996/libcxx/test/std/experimental/reflection/p2996-ex-getting-class-layout.pass.cpp
2025-06-24 11:01:59 -04:00

60 lines
1.4 KiB
C++

//===----------------------------------------------------------------------===//
//
// Copyright 2024 Bloomberg Finance L.P.
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03 || c++11 || c++14 || c++17 || c++20
// ADDITIONAL_COMPILE_FLAGS: -freflection
// ADDITIONAL_COMPILE_FLAGS: -Wno-inconsistent-missing-override
// <experimental/reflection>
//
// [reflection]
#include <meta>
#include <array>
#include <cstdlib>
#include <print>
struct member_descriptor
{
std::size_t offset;
std::size_t size;
};
// returns std::array<member_descriptor, N>
template <typename S>
consteval auto get_layout() {
constexpr auto ctx = std::meta::access_context::current();
auto members = nonstatic_data_members_of(^^S, ctx);
constexpr size_t sz = nonstatic_data_members_of(^^S, ctx).size();
std::array<member_descriptor, sz> layout;
for (int i = 0; i < members.size(); ++i) {
layout[i] = {
.offset=static_cast<std::size_t>(offset_of(members[i]).bytes),
.size=size_of(members[i])
};
}
return layout;
}
struct X
{
char a;
int b;
double c;
};
int main() {
for (const auto &md : get_layout<X>())
std::println("({}, {})", md.offset, md.size);
}