直接贴代码,注意MSVC需要将alignments前的constexpr去掉才能过编译。godbot

其实只用了boost::pfr::structure_to_tuple()这一个函数,有关将POD类型转化为tuple的原理可参见C++14实现编译期反射–剖析magic_get中的magic

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

// Pretty-printer for `struct` layout and padding bytes
// by Vittorio Romeo (@supahvee1234) (https://vittorioromeo.info)

#include <boost/pfr.hpp>
#include <iostream>
#include <tuple>
#include <utility>
#include <type_traits>
#include <typeinfo>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <array>

namespace detail
{
    // ------------------------------------------------------------------------------
    // Round `x` up to the nearest multiple of `mult`.
    [[nodiscard]] constexpr std::size_t round_up(const std::size_t x,
        const std::size_t mult) noexcept
    {
        return ((x + mult - 1) / mult) * mult;
    }

    // ------------------------------------------------------------------------------
    // Recursively print the memory layout of `T` using identation `indent` and
    // keeping track of the total occupied bytes in `used`.
    template <typename T>
    void print_layout_impl(const std::size_t indent, std::size_t& used)
    {
        // ------------------------------------------------------------------------------
        // Utilities.
        const char* const t_name = typeid(T).name();
        const std::size_t line_length = std::strlen(t_name) + 32;

        const auto print_indent = [&](const std::size_t spacing = 1)
        {
            for (std::size_t i = 0; i <= indent; ++i) { std::cout << ((i % 2 == 0) ? '|' : ' '); }
            for (std::size_t i = 0; i < spacing; ++i) { std::cout << ' '; }
        };

        const auto print_line = [&]
        {
            print_indent(0);
            for (std::size_t i = 0; i < line_length; ++i) { std::cout << '-'; }
            std::cout << '\n';
        };

        // ------------------------------------------------------------------------------
        // Tuple type of all data members of `T`, in order. Used to reflect on `T`.
        using tuple_type = decltype(boost::pfr::structure_to_tuple(std::declval<T>()));

        // ------------------------------------------------------------------------------
        // We use a pointer to `std::tuple` to support non-default-constructible types.
        // We need this inner lambda so that we can use `Ts...` as a pack.
        [&]<typename... Ts>(std::tuple<Ts...>*)
        {
            // ------------------------------------------------------------------------------
            // All alignments of the data members, with the alignment of `T` at the end.

            constexpr std::array alignments{ alignof(Ts)..., alignof(T) };
            //std::array alignments{ alignof(Ts)..., alignof(T) };   MSVC

            // ------------------------------------------------------------------------------
            // Information printed in the header.
            constexpr std::size_t sum_of_member_sizes = (sizeof(Ts) + ...);
            constexpr std::size_t total_padding_bytes = (sizeof(T) - sum_of_member_sizes);

            // ------------------------------------------------------------------------------
            // Print the header.
            print_line();
            print_indent();

            std::cout << t_name
                << " {size: " << sizeof(T) << " ("
                << sum_of_member_sizes << "# " << total_padding_bytes
                << "p), align: " << alignof(T) << "}\n";

            print_line();

            std::size_t type_idx = 0;

            // -----------------------------------------------------------------------------
            // Print padding in relation to a given alignment
            const auto print_padding = [&](const std::size_t alignment)
            {
                const std::size_t padding = round_up(used, alignment) - used;
                for (int i = 0; i < padding; ++i) { std::cout << 'p'; }
                used += padding;
            };

            // -----------------------------------------------------------------------------
            // Non-recursively print a fundamental/pointer/reference type
            const auto print_fundamental = [&]<typename X>
            {
                print_indent();
                std::cout << std::setw(2) << used << ": [";

                print_padding(alignments[type_idx]);

                for (std::size_t i = 0; i < sizeof(X); ++i) { std::cout << '#'; }
                used += sizeof(X);

                print_padding(alignments[type_idx + 1]);

                std::cout << "] " << typeid(X).name() << '\n';
            };

            // -----------------------------------------------------------------------------
            // Recursively print all data members
            ([&]
            {
                if constexpr (std::is_fundamental_v<Ts>
                    || std::is_pointer_v<Ts>
                    || std::is_reference_v<Ts>)
                {
                    print_fundamental.template operator() < Ts > ();
                }
                else
                {
                    print_layout_impl<Ts>(indent + 2, used);
                }

                ++type_idx;
            }(), ...);
        }(static_cast<tuple_type*>(nullptr));

        print_line();
    }
}

template <typename T>
void print_layout()
{
    std::size_t used = 0;
    detail::print_layout_impl<T>(0 /* indent */, used /* used */);
}

int main()
{
    struct foo1
    {
        char* p;     /* 8 bytes */
        char c;      /* 1 byte
        char pad[7];    7 bytes */
        long x;      /* 8 bytes */
    };

    print_layout<foo1>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct foo2
    {
        char  c;      /* 1 byte
        char  pad[7];    7 bytes */
        char* p;      /* 8 bytes */
        long  x;      /* 8 bytes */
    };

    print_layout<foo2>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct foo3
    {
        char* p;      /* 8 bytes */
        char  c;      /* 1 byte
        char  pad[7];    7 bytes */
    };

    print_layout<foo3>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct foo4
    {
        short s;      /* 2 bytes */
        char  c;      /* 1 byte
        char  pad[1];    1 byte */
    };

    print_layout<foo4>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct foo5
    {
        char c;           /* 1 byte
        char pad1[7];        7 bytes */

        struct foo5_inner
        {
            char* p;       /* 8 bytes */
            short x;       /* 2 bytes
            char  pad2[6];    6 bytes */
        } inner;
    };

    print_layout<foo5::foo5_inner>();
    std::cout << '\n';

    print_layout<foo5>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct foo10
    {
        char   c;       /* 1 byte
        char   pad1[7];    7 bytes */
        foo10* p;       /* 8 bytes */
        short  x;       /* 2 bytes
        char   pad2[6];    6 bytes */
    };

    print_layout<foo10>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct foo11
    {
        foo11* p;      /* 8 bytes */
        short  x;      /* 2 bytes */
        char   c;      /* 1 byte
        char   pad[5];    5 bytes */
    };

    print_layout<foo11>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct test0
    {
        int   i;
        char  c;
        float f;
    };

    print_layout<test0>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct test1
    {
        int    i;
        double d;
        char   c;
        float  f;
    };

    print_layout<test1>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct test2
    {
        void* p0;
        test0 t0;
        void* p1;
        test1 t1;
        void* p2;
    };

    print_layout<test2>();
    std::cout << '\n';

    // ------------------------------------------------------------------------------

    struct test2_flat
    {
        void* p0;
        int    i0;
        char   c0;
        float  f0;
        void* p1;
        int    i1;
        double d0;
        char   c1;
        float  f1;
        void* p2;
    };

    print_layout<test2_flat>();
    std::cout << '\n';
}

输出如下

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|--------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo1 {size: 16 (13# 3p), align: 8}
|--------------------------------------------------------------------------
|  0: [########] char * __ptr64
|  8: [#ppp] char
| 12: [####] long
|--------------------------------------------------------------------------

|--------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo2 {size: 24 (13# 11p), align: 8}
|--------------------------------------------------------------------------
|  0: [#ppppppp] char
|  8: [########] char * __ptr64
| 16: [####pppp] long
|--------------------------------------------------------------------------

|--------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo3 {size: 16 (9# 7p), align: 8}
|--------------------------------------------------------------------------
|  0: [########] char * __ptr64
|  8: [#ppppppp] char
|--------------------------------------------------------------------------

|--------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo4 {size: 4 (3# 1p), align: 2}
|--------------------------------------------------------------------------
|  0: [##] short
|  2: [#p] char
|--------------------------------------------------------------------------

|--------------------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo5::foo5_inner {size: 16 (10# 6p), align: 8}
|--------------------------------------------------------------------------------------
|  0: [########] char * __ptr64
|  8: [##pppppp] short
|--------------------------------------------------------------------------------------

|--------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo5 {size: 24 (17# 7p), align: 8}
|--------------------------------------------------------------------------
|  0: [#ppppppp] char
| |--------------------------------------------------------------------------------------
| | struct `int __cdecl main(void)'::`2'::foo5::foo5_inner {size: 16 (10# 6p), align: 8}
| |--------------------------------------------------------------------------------------
| |  8: [########] char * __ptr64
| | 16: [##pppppp] short
| |--------------------------------------------------------------------------------------
|--------------------------------------------------------------------------

|---------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo10 {size: 24 (11# 13p), align: 8}
|---------------------------------------------------------------------------
|  0: [#ppppppp] char
|  8: [########] struct `int __cdecl main(void)'::`2'::foo10 * __ptr64
| 16: [##pppppp] short
|---------------------------------------------------------------------------

|---------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::foo11 {size: 16 (11# 5p), align: 8}
|---------------------------------------------------------------------------
|  0: [########] struct `int __cdecl main(void)'::`2'::foo11 * __ptr64
|  8: [##] short
| 10: [#ppppp] char
|---------------------------------------------------------------------------

|---------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::test0 {size: 12 (9# 3p), align: 4}
|---------------------------------------------------------------------------
|  0: [####] int
|  4: [#ppp] char
|  8: [####] float
|---------------------------------------------------------------------------

|---------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::test1 {size: 24 (17# 7p), align: 8}
|---------------------------------------------------------------------------
|  0: [####pppp] int
|  8: [########] double
| 16: [#ppp] char
| 20: [####] float
|---------------------------------------------------------------------------

|---------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::test2 {size: 64 (60# 4p), align: 8}
|---------------------------------------------------------------------------
|  0: [########] void * __ptr64
| |---------------------------------------------------------------------------
| | struct `int __cdecl main(void)'::`2'::test0 {size: 12 (9# 3p), align: 4}
| |---------------------------------------------------------------------------
| |  8: [####] int
| | 12: [#ppp] char
| | 16: [####] float
| |---------------------------------------------------------------------------
| 20: [pppp########] void * __ptr64
| |---------------------------------------------------------------------------
| | struct `int __cdecl main(void)'::`2'::test1 {size: 24 (17# 7p), align: 8}
| |---------------------------------------------------------------------------
| | 32: [####pppp] int
| | 40: [########] double
| | 48: [#ppp] char
| | 52: [####] float
| |---------------------------------------------------------------------------
| 56: [########] void * __ptr64
|---------------------------------------------------------------------------

|--------------------------------------------------------------------------------
| struct `int __cdecl main(void)'::`2'::test2_flat {size: 64 (50# 14p), align: 8}
|--------------------------------------------------------------------------------
|  0: [########] void * __ptr64
|  8: [####] int
| 12: [#ppp] char
| 16: [####pppp] float
| 24: [########] void * __ptr64
| 32: [####pppp] int
| 40: [########] double
| 48: [#ppp] char
| 52: [####] float
| 56: [########] void * __ptr64
|--------------------------------------------------------------------------------