Click to See Complete Forum and Search --> : structure alignment in C++09


hendrixj
05-06-2009, 01:07 PM
Does C++ 09 give ways to pack structures or specify data item alignment?
Many compilers offer a way to pack structs, but they aren't part of the language. I was thinking Danny or someone said that C++09 might fill in this gap.

Danny
05-06-2009, 04:35 PM
Yes it does. A new feature called attributes which looks like this:
[[attribute]] will allow you to control the alignment of aggregates and classes.
Originally, the C++0x draft had two new keywords: alignof and alignas (which were described in detail in a former C++ 10 minute solution: http://www.devx.com/cplus/10MinuteSolution/38542/1954 ). However, as it happens often when a new standard is being drafted, those keywords were replaced with the more general notion of attributes, which can be used for other purposes.
I will dedicate an article to this issue soon.

Peter_APIIT
05-09-2009, 04:58 AM
Good job.

Danny
05-20-2009, 10:58 AM
This is how attributes are used in C++09:
http://www.devx.com/cplus/Article/41694/0/page/1

hendrixj
05-20-2009, 01:14 PM
Thanks for the article.

Will there be a "packed" attribute, similar to gcc's __attribute__((packed))?

Does the [[align]] attribute allow you to "pack" an item? That is, could I use align(1) to force a double right after a char?

Danny
05-20-2009, 07:31 PM
Whether "packing" is allowed is implementation-dependent of course but at least the syntax is:
struct S[[align(char)]]{
/**/
};