DarthRubik
DarthRubik

Reputation: 3975

std method for packing data

Suppose I have a struct that I want packed (because I am communicating with another program, and it is simpler to just pack all of the data so that you can guarantee it meshes).

In gcc to pack a struct you do this:

struct __attribute((packed)) Mine { /* Members here */ };

Is there a template defined in the std library to do this?

The reason I ask is because there is std::aligned_storage, which is extremely helpful on the alignment end of things, and it seems logical to have a similar thing for packing data.

So does this exist?

Upvotes: 1

Views: 203

Answers (2)

SergeyA
SergeyA

Reputation: 62553

No, it doesn't exist. Standard explicitly says that packing fields in struct is implementation defined.

It also mentions that for bit fields the way bits flow from one bit field to next is also implementation defined (I get that you are using bitfields too). Sorry, but you'd have to code explicitly for your compiler(s).

Upvotes: 2

MSalters
MSalters

Reputation: 179779

While it's hard to prove a negative, I'm sure the answer is no.

Fundamentally, std is almost entirely implementable in fairly well-defined C++, with only the occasional need for limited compiler coordination (e.g. std::size_t is the type of sizeof expressions). The feature you describe would be fully at odds with this notion.

Upvotes: 0

Related Questions