12

Is there a way to write a compile-time assertion that checks if some type has any padding in it?

For example:

struct This_Should_Succeed
{
    int a;
    int b;
    int c;
};

struct This_Should_Fail
{
    int a;
    char b;
    // because there are 3 bytes of padding here
    int c;
};
13
  • Only with a macro/template that has every member variable passed to it. But what are you really trying to do by insuring that all your structs are free of padding?
    – selbie
    Sep 7, 2019 at 0:50
  • 1
    @selbie I have some functions that work on void* memory (for hashing, comparing, .. stuff like that), and there are cases where I would like to avoid boilerplate of writing special hash/compare functions for a type; I would like to just hash and compare the bytes. But it's unsafe to do that if a struct has padding in it because values of those bytes are undefined.
    – Kalinovcic
    Sep 7, 2019 at 0:53
  • If you reliably zero'd out the bytes of the struct before initializing its members, then wouldn't all your hashing and comparing be consistent? (Disclaimer, someone is going to chide me about undefined behavior for making that suggestion.)
    – selbie
    Sep 7, 2019 at 0:54
  • 2
    Nothing today. Maybe in C++23. There's a lot of investment into exploring compile time reflection for C++. Sep 7, 2019 at 0:57
  • 1
    @selbie that has a pretty significant performance cost (and also breaks compatibility with processors that don't support unaligned loads and stores), but it also doesn't solve my problem because I don't know up front what type I'm checking, the type is a template input.
    – Kalinovcic
    Sep 7, 2019 at 1:00

4 Answers 4

9

Since C++17 you might be able to use std::has_unique_object_representations.

#include <type_traits>

static_assert(std::has_unique_object_representations_v<This_Should_Succeed>); // succeeds
static_assert(std::has_unique_object_representations_v<This_Should_Fail>); // fails

Although, this might not do exactly what you want it to do. For instance, it could fail if your struct contains

  • a bool in an ABI where any nonzero value of a byte is true, not just 0x01, or
  • an IEC 559 float because there are multiple representations of zero and NaN, or
  • a pointer on an architecture with multiple null pointer representations, or
  • any fundamental type with padding, or
  • other such types that don't have unique object representations.

Check the linked cppreference page for details, and see What type will make "std::has_unique_object_representations" return false?

3
  • 3
    Although note that this will be fail for all structs that contain floats (on IEEE-754 compliant systems) whether they contain padding or not.
    – eerorika
    Sep 7, 2019 at 1:44
  • also note that it won't work for non-TriviallyCopyable structs
    – phuclv
    Sep 7, 2019 at 8:53
  • 1
    Wow, this is very close to what I wanted. I would need it to work for structs with floats too, though, but I had no idea that this exists. Thanks!
    – Kalinovcic
    Sep 7, 2019 at 14:05
6

Edit: Check Indiana's answer.

Is there a way to write a compile-time assertion that checks if some type has any padding in it?

Yes.

You can sum the sizeof of all members and compare it to the size of the class itself:

static_assert(sizeof(This_Should_Succeed) == sizeof(This_Should_Succeed::a)
                                           + sizeof(This_Should_Succeed::b)
                                           + sizeof(This_Should_Succeed::c));

static_assert(sizeof(This_Should_Fail)    != sizeof(This_Should_Fail::a)
                                           + sizeof(This_Should_Fail::b)
                                           + sizeof(This_Should_Fail::c));

This unfortunately requires explicitly naming the members for the sum. An automatic solution requires (compile time) reflection. Unfortunately, C++ language has no such feature yet. Maybe in C++23 if we are lucky. For now, there are solutions based on wrapping the class definition in a macro.

A non-portable solution might be to use -Wpadded option provided by GCC, which promises to warn if structure contains any padding. This can be combined with #pragma GCC diagnostic push to only do it for chosen structures.


type I'm checking, the type is a template input.

A portable, but not fully satisfactory approach might be to use a custom trait that the user of the template can use to voluntarily promise that the type does not contain padding allowing you to take advantage of the knowledge.

The user would have to rely on explicit or pre-processor based assertion that their promise holds true.

5
  • 2
    This isn't really enough either. If any members are structures themselves, I would also have to enumerate their members, etc... I have my own preprocessor so I can make a check like this by generating that code, I was just wondering if there's any way to do it with vanilla C++. Thanks, this seems to be the best thing currently possible!
    – Kalinovcic
    Sep 7, 2019 at 1:11
  • @Kalinovcic yeah, if padding within members is important, then this check would have to be done recursively for each member. Doing that with standard pre-processor would be beyond my skill, but with a custom one it might be quite doable.
    – eerorika
    Sep 7, 2019 at 1:12
  • @Justin The second thing doesn't work for me because it's too intrusive. The first suggestion is interesting, I guess it would work and it's interesting that it's possible, but I'm terrified of the extra compilation time when all those headers get included. :) It's way too much code for a very simple check, I would rather do it with a custom preprocessor in that case.
    – Kalinovcic
    Sep 7, 2019 at 14:33
  • 1
    @Kalinovcic There are techniques to emulate reflection without the preprocessor. One is the magic-get library which works for structs; it can walk the members of the struct. Another technique is to have any reflectable type have a auto reflect() const { return std::tie(a, b, c); } or static auto members() { return std::tuple(&TheType::a, &TheType::b, &TheType::c); }
    – Justin
    Sep 7, 2019 at 14:34
  • @Justin I'm doing this to avoid the boilerplate of requiring hash and compare functions for types, so requiring a member list function doesn't seems like a good solution. It's also very likely to become outdated as the structure changes.
    – Kalinovcic
    Sep 7, 2019 at 14:38
1

To get the total field size without retyping each struct member you can use an X Macro

First define all the fields

#define LIST_OF_FIELDS_OF_This_Should_Fail    \
    X(int, a)          \
    X(char, b)         \
    X(int, c)

#define LIST_OF_FIELDS_OF_This_Should_Succeed \
    X(long long, a)    \
    X(long long, b)    \
    X(int, c)          \
    X(int, d)          \
    X(int, e)          \
    X(int, f)

then declare the structs

struct This_Should_Fail {
#define X(type, name) type name;
    LIST_OF_FIELDS_OF_This_Should_Fail
#undef X
};

struct This_Should_Succeed {
#define X(type, name) type name;
    LIST_OF_FIELDS_OF_This_Should_Succeed
#undef X
};

and check

#define X(type, name) sizeof(This_Should_Fail::name) +
static_assert(sizeof(This_Should_Fail) == LIST_OF_FIELDS_OF_This_Should_Fail 0);
#undef X

#define X(type, name) sizeof(This_Should_Succeed::name) +
static_assert(sizeof(This_Should_Succeed) == LIST_OF_FIELDS_OF_This_Should_Succeed 0);
#undef X

or you can just reuse the same X macro to check

#define X(type, name) sizeof(a.name) +
{
    This_Should_Fail a;
    static_assert(sizeof(This_Should_Fail) == LIST_OF_FIELDS_OF_This_Should_Fail 0);
}
{
    This_Should_Succeed a;
    static_assert(sizeof(This_Should_Succeed) == LIST_OF_FIELDS_OF_This_Should_Succeed 0);
}        
#undef X

See demo on compiler explorer

For more information about this you can read Real-world use of X-Macros

1
  • I'm aware of X-macros, but I would like to avoid macro hell solutions like these. I have my own custom preprocessor, so I could make something like this nicer using that. I was looking for a non-intrusive solution, but I guess I didn't explain that well enough. Thanks!
    – Kalinovcic
    Sep 7, 2019 at 14:10
1

An alternate non-portable solution is to compare the size of the struct with a packed version with #pragma pack or __attribute__((packed)). #pragma pack is also supported by many other compilers like GCC or IBM XL

#ifdef _MSC_VER
#define PACKED_STRUCT(declaration) __pragma(pack(push, 1)) declaration __pragma(pack(pop))
#else
#define PACKED_STRUCT(declaration) declaration __attribute((packed))
#endif

#define THIS_SHOULD_FAIL(name) struct name \
{                        \
    int a;               \
    char b;              \
    int c;               \
}

PACKED_STRUCT(THIS_SHOULD_FAIL(This_Should_Fail_Packed));
THIS_SHOULD_FAIL(This_Should_Fail);

static_assert(sizeof(This_Should_Fail_Packed) == sizeof(This_Should_Fail));

Demo on Compiler Explorer

See Force C++ structure to pack tightly. If you want to have an even more portable pack macro then try this

Related:


In GCC and Clang there's a -Wpadded option for this purpose

  • -Wpadded

    Warn if padding is included in a structure, either to align an element of the structure or to align the whole structure. Sometimes when this happens it is possible to rearrange the fields of the structure to reduce the padding and so make the structure smaller.


In case the struct is in a header that you can't modify then in some cases it can be worked around like this to get a packed copy of the struct

#include "header.h"

// remove include guard to include the header again
#undef HEADER_H

// Get the packed versions
#define This_Should_Fail This_Should_Fail_Packed
#define This_Should_Succeed  This_Should_Succeed_Packed

// We're including the header again, so it's quite dangerous and
// we need to do everything to prevent duplicated identifiers:
// rename them, or define some macros to remove possible parts

#define someFunc someFunc_deleted
// many parts are wrapped in SOME_CONDITION so this way
// we're preventing them from being redeclared
#define SOME_CONDITION 0

#pragma pack(push, 1)
#include "header.h"
#pragma pack(pop)

#undef This_Should_Fail
#undef This_Should_Succeed

static_assert(sizeof(This_Should_Fail_Packed) == sizeof(This_Should_Fail));
static_assert(sizeof(This_Should_Succeed_Packed) == sizeof(This_Should_Succeed ));

This won't work for headers that use #pragma once or some structs that include structs in other headers though

4
  • This would work well if there was a way to pack a struct somewhere other than at the declaration site (like producing a "packed copy" of a struct). I'm not aware of such a thing though.
    – Kalinovcic
    Sep 7, 2019 at 14:13
  • yeah, unfortunately you have to do that at the declaration site. However for some simple headers you can work around that. See my edit
    – phuclv
    Sep 7, 2019 at 15:06
  • I would like to avoid packing the actual struct, because that can have a pretty big performance impact, and breaks compatibility with cpu architectures that don't support unaligned loads and stores. I don't want to force a type to be compatible with my functions, I just want to disallow types that aren't.
    – Kalinovcic
    Sep 7, 2019 at 15:11
  • 1
    Ah, sorry, I misread your code, I see you're including the header twice. Yes, this would work. It's a lot of setup and boilerplate though, it misses the original point of avoiding boilerplate for each type.
    – Kalinovcic
    Sep 7, 2019 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.