With Clang 3.4 I get the same destruction order for both std::pair
and 2 element std::tuple
and with g++ 5.3 I get opposite order which could be mainly due to the recursive implementation of std::tuple
in libstd++.
So, it basically boils down to what I said in the comment, it is implementation defined.
From the BUG report:
Comment by Martin Sebor
Since the layout of std::pair members is fully specified, so is the
order of their initialization and destruction. The output of the test
case reflects this order.
The order of initialization (and destruction) of std:stuple subobjects
is less clearly specified. At least it's not immediately obvious from
my reading of the spec if any particular order is required.
The reason why the output for std::tuple with libstdc++ is the reverse
of std::pair is because the implementation, which relies on recursive
inheritance, stores and constructs tuple elements in the reverse
order: i.e., the base class, which stores the last element, is stored
and constructed first, followed by each derived class (each of which
stores the last - Nth element).
The quote from standard [section 20.4.1] which the bug reporter is quoting to
1 This subclause describes the tuple library that provides a tuple
type as the class template tuple that can be instantiated with any
number of arguments. Each template argument specifies the type of an
element in the tuple. Consequently, tuples are heterogeneous,
fixed-size collections of values. An instantiation of tuple with two
arguments is similar to an instantiation of pair with the same two
arguments. See 20.3.
Argument against this made in the linked bug is:
Being described as similar doesn't imply they are identical in every
detail. std::pair and std::tuple are distinct classes with different
requirements on each. If you believe the are required to behave
identically in this respect (i.e., have their subobjects defined in
the same order) you need to point to the specific wording that
guarantees it.
std::tuple
is implemented in your standard library. – Osteomastd::tuple
. Probably should be filed as unspecified. – Sung