Does the tuple implementation have an optimized layout?

When reading this, I was amazed at what a certain level of metaprogramming can do for your class. I must admit that I do not quite understand what the proposed optimal layout, if I were to indicate that I understood, it would be as follows:

ordering a class member using downward alignment, i.e. the type with the highest result alignof comes first, etc.

Feel free to correct me if I am wrong (if you have a brief explanation of why this is happening, it would be even better, I could not copy a bunch of large pieces of justification in my question), but my question is: on another topic:

Does such an implementation of the std::tuple library have such layout optimization?

If not, are there any standard types of algebraic data, is there any other way to do this for my class other than writing such a mechanism?

+6
source share
1 answer

There is no library implementation. I know that optimizes the layout for alignment. You can use such a program to check the tuple layout:

 #include <iostream> #include <tuple> struct empty {}; int main() { using T = std::tuple<double, int, empty, short, long>; T t{}; std::cout << &t << '\n'; std::cout << &std::get<0>(t) << '\n'; std::cout << &std::get<1>(t) << '\n'; std::cout << &std::get<2>(t) << '\n'; std::cout << &std::get<3>(t) << '\n'; std::cout << &std::get<4>(t) << '\n'; std::cout << &t+1 << '\n'; std::cout << sizeof(T) << '\n'; } 

libC ++ stores elements in the order of declaration and optimizes space for empty elements. Empty members are shunted in the direction of the front. Output Example:

 0x7fff5ccf39f8 0x7fff5ccf39f8 0x7fff5ccf3a00 0x7fff5ccf39f8 0x7fff5ccf3a04 0x7fff5ccf3a08 0x7fff5ccf3a10 24 

libstdC ++ stores elements in the reverse order of declarations and optimizes space for empty elements. Empty members are shunted in the direction of the front. Output Example:

 0x7ffe4fc5b2a0 0x7ffe4fc5b2b0 0x7ffe4fc5b2ac 0x7ffe4fc5b2a0 0x7ffe4fc5b2a8 0x7ffe4fc5b2a0 0x7ffe4fc5b2b8 24 

VS-2015 stores elements in the reverse order of declarations and does not optimize space for empty elements. Output Example:

 0306FEF4 0306FF04 0306FF00 0306FEFC 0306FEF8 0306FEF4 0306FF0C 24 

In this example, we see that optimizing the space for an empty element didnโ€™t buy anything, since it is still suitable for the fill area.

There are no tools that automate the task of reducing fill in the standard.

+9
source

All Articles