public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements
@ 2015-06-29 21:10 antoshkka at gmail dot com
  2015-06-29 21:34 ` [Bug libstdc++/66699] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: antoshkka at gmail dot com @ 2015-06-29 21:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66699

            Bug ID: 66699
           Summary: Incorrect order of destruction for std::tuple elements
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

According to C++ Standard:
"An instantiation of tuple with two arguments is similar to an instantiation of
pair with the same two arguments."

The following small example prints the order of tuple and pair elements
destruction:

#include <tuple>
#include <iostream>

struct print_num {
    int num_;
    ~print_num() {
        std::cerr << num_;
    }
};


int main() {
    {
        std::cerr << "pair : ";
        std::pair<print_num, print_num> p;
        std::get<0>(p).num_ = 0;
        std::get<1>(p).num_ = 1;
    }

    {
        std::cerr << "\ntuple: ";
        std::tuple<print_num, print_num> t;
        std::get<0>(t).num_ = 0;
        std::get<1>(t).num_ = 1;
    }
}

Program outputs
pair : 10
tuple: 01


It seems that destruction of a tuple from last to first element is more correct
than the current approach with destruction from the first element to last.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/66699] Incorrect order of destruction for std::tuple elements
  2015-06-29 21:10 [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements antoshkka at gmail dot com
@ 2015-06-29 21:34 ` pinskia at gcc dot gnu.org
  2015-06-30  3:09 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2015-06-29 21:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66699

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I don't see why you think this is an issue because:
f(temp(), temp());

The C++ does not specifies which one gets constructed first or second.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/66699] Incorrect order of destruction for std::tuple elements
  2015-06-29 21:10 [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements antoshkka at gmail dot com
  2015-06-29 21:34 ` [Bug libstdc++/66699] " pinskia at gcc dot gnu.org
@ 2015-06-30  3:09 ` msebor at gcc dot gnu.org
  2015-06-30  5:36 ` antoshkka at gmail dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2015-06-30  3:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66699

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
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).


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/66699] Incorrect order of destruction for std::tuple elements
  2015-06-29 21:10 [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements antoshkka at gmail dot com
  2015-06-29 21:34 ` [Bug libstdc++/66699] " pinskia at gcc dot gnu.org
  2015-06-30  3:09 ` msebor at gcc dot gnu.org
@ 2015-06-30  5:36 ` antoshkka at gmail dot com
  2015-06-30 16:47 ` msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: antoshkka at gmail dot com @ 2015-06-30  5:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66699

--- Comment #3 from Antony Polukhin <antoshkka at gmail dot com> ---
(In reply to Martin Sebor from comment #2)
> 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.

I'm pointing out that according to Standard std::pair<First, Second> and
std::tuple<First, Second> must be similar. std::pair destruction order is fully
specified, so that order must be used by tuple too.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/66699] Incorrect order of destruction for std::tuple elements
  2015-06-29 21:10 [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements antoshkka at gmail dot com
                   ` (2 preceding siblings ...)
  2015-06-30  5:36 ` antoshkka at gmail dot com
@ 2015-06-30 16:47 ` msebor at gcc dot gnu.org
  2015-06-30 17:00 ` redi at gcc dot gnu.org
  2015-06-30 17:06 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2015-06-30 16:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66699

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/66699] Incorrect order of destruction for std::tuple elements
  2015-06-29 21:10 [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements antoshkka at gmail dot com
                   ` (3 preceding siblings ...)
  2015-06-30 16:47 ` msebor at gcc dot gnu.org
@ 2015-06-30 17:00 ` redi at gcc dot gnu.org
  2015-06-30 17:06 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2015-06-30 17:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66699

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Antony Polukhin from comment #3)
> std::pair destruction order is
> fully specified, so that order must be used by tuple too.

No, the standard says no such thing.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/66699] Incorrect order of destruction for std::tuple elements
  2015-06-29 21:10 [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements antoshkka at gmail dot com
                   ` (4 preceding siblings ...)
  2015-06-30 17:00 ` redi at gcc dot gnu.org
@ 2015-06-30 17:06 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2015-06-30 17:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66699

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
And that's not even what similar means anyway.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-06-30 17:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-29 21:10 [Bug libstdc++/66699] New: Incorrect order of destruction for std::tuple elements antoshkka at gmail dot com
2015-06-29 21:34 ` [Bug libstdc++/66699] " pinskia at gcc dot gnu.org
2015-06-30  3:09 ` msebor at gcc dot gnu.org
2015-06-30  5:36 ` antoshkka at gmail dot com
2015-06-30 16:47 ` msebor at gcc dot gnu.org
2015-06-30 17:00 ` redi at gcc dot gnu.org
2015-06-30 17:06 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).