Why does libstdc++ store std::tuple elements in reverse order?
Asked Answered
A

1

10

According to Link, with regards to std::tuple...

libstdc++ always places the members in reverse order, and libc++ always places the members in the order given

Assuming that's true, is there a reason (historical or otherwise) why libstdc++ uses reverse order?

Bonus: Has either implementation ever changed its std::tuple ordering for any reason?

Autostability answered 27/12, 2014 at 1:36 Comment(3)
I'm not aware of either library breaking its ABI, and reversing that order would absolutely break the ABI.Sinusoid
It hadn't even occurred to me that changing the order would break the ABI, but now it seems obvious. I guess that answers that part of the question. Thanks.Autostability
I made a change to the libstdc++ tuple layout yesterday (see PR 56785) but only to remove some empty base classes and make it more compact. The ordering of elements didn't change.Shoe
H
12

See this answer for why libc++ chose forward order. As for why libstdc++ chose reverse order, that is probably because that's how it was demonstrated in the variadics template proposal, and is the more obvious implementation.

Bonus: No. These orderings have been stable in both libraries.

Update

libc++ chose forward storage order because:

  1. It is implementable.
  2. The implementation has good compile-time performance.
  3. It gives clients of libc++ something that is intuitive and controllable, should they care about the order of the storage, and are willing to depend on it while using libc++, despite its being unspecified.

In short, the implementor of the libc++ tuple merely felt that storing the objects in the order the client (implicitly) specified was the quality thing to do.

Honorable answered 27/12, 2014 at 1:39 Comment(1)
Maybe it's because I don't grok TMP at all, but I don't see where the linked post says why libc++ chose forward order. It explains why a non-recursive tuple implementation is good (which makes sense to me in principle, though I find the linked libc++ code for it totally unreadable) and then mentions at the end that libc++ uses forward order without any explanation. Is there meant to be an implication that forward order is the "obvious implementation" of a non-recursive tuple?Autostability

© 2022 - 2024 — McMap. All rights reserved.