Is there any way to enforce certain size(of) class C by adding appropriate space/padding after its attributes?
Your best option would probably be to use a union
. Roughly speaking, with some details missing:
class C
{
int foo;
};
union CAlloc
{
C instance;
std::byte enforced_size[DESIRED_SIZE];
};
Then use CAlloc::instance
where needed.
Update: as @Mgetz pointed out in the comments, std::byte
is a C++17 addition, you may want/need to use char
if you're using an older standard.
C++11 has introduced new keyword: alignas I think this is something more handy from your point of view.