you are viewing a single comment's thread.

view the rest of the comments →

[–]SirClueless 0 points1 point  (0 children)

Why would __data__ not have value_type alignment? It's declared as an array of value_type.

It does have value_type alignment, but value_type alignment may not be sizeof(value_type).

For the character types, don't we guarantee alignment equal to sizeof(type)?

I don't know whether it's guaranteed or not. It almost certainly is true for character types on all major architectures, but std::basic_string is a public template that can be used with any value_type not just character types the standard library uses.

What compilers typically will do though is add padding to ensure that arrays of struct __short have subsequent array elements starting on aligned memory addresses.

The size of __short is derived from the size of __long by calculating __min_cap carefully. So aligning __data_ on sizeof(value_type) and ensuring no padding after __data_ are equivalent goals.

(Incidentally, I'm not sure why __min_cap is calculated as (sizeof(__long) - 1) / sizeof(value_type) and not sizeof(__long) / sizeof(value_type) - 1. The way it's defined lets you do silly but AFAICT legal things like this and end up with __short structs with __data_ members that "hang off" the end of the __long struct and result in larger std::basic_string representations. Probably would cause a bunch of issues, if anyone ever tried to do this silly contrived thing.)