dimanche 23 août 2015

How can I arbitrarily sort a tuple's types?

One thing that really annoys me about C++ is that an empty struct/class takes up space. The idea that the problems associated with having 0 sized items was 'finessed' out by making them always have a size greater than 0 seems to me as the wrong way to go, since if you wanted to be able to track a type based on it's address, you can 'finesse' the problem out in other ways that would IMHO be far more sane, such as adding a pad variable or wrapping the item in a template with a pad variable. Instead, we got empty base class optimization, which is kludgey, completely compiler implementation dependent (because as an optimization it is not part of the standard), and thus can be far more difficult to manage portably.

So, I have this idea that std::tuple (or some variant, since it's (and the compiler's) implementation is highly implementation dependent) might be able to save the day, which it sort of does, but there are issues due to packing and alignment. Because of how compilers will align the items in the struct, having a empty next to a non-empty next to an empty next to a non-empty will be larger than 2 empties next to 2 non-empties.

Because of this, I need a way to reorder the types based on some criteria. Sorting the entire list based on size isn't necessary (and may in some cases be detrimental) so I need some generic way to reorder the tuple's type list but still access it as if the type list was in the original order.

I looked around a bit and haven't found anything like this and I'm at a loss. Ideas on how to accomplish this?

Aucun commentaire:

Enregistrer un commentaire