public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107531] New: List of references calls destructors, add warning?
@ 2022-11-05  5:16 nightstrike at gmail dot com
  2022-11-05  5:20 ` [Bug c++/107531] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: nightstrike at gmail dot com @ 2022-11-05  5:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107531
           Summary: List of references calls destructors, add warning?
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nightstrike at gmail dot com
  Target Milestone: ---

The following code results in a destructor being calling twice:

#include <initializer_list>
struct S {
    S() { __builtin_printf("ctor\n"); }
    ~S() { __builtin_printf("dtor\n"); }
};

template<typename ... Args>
void f(Args const & ... args) {
    for (auto const & s: { args... })
        ;
}

int main() {
    S s;
    f(s);
}

$ ./a.out
ctor
dtor
dtor


Changing line 9 to use pointers fixes it:

for (auto const * s: { &args... })

$ ./a.out
ctor
dtor


I think that if my code is exploiting some kind of bad behavior (and it likely
is), a warning at -Wall would be a nice addition (currently, there is none). 
If what I'm doing is allowed, I don't think calling a destructor twice is the
best idea :) :).  I'd be betting money on the first thing, though.  Maybe
adding something from -fsanitize=undefined would be an option?

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

end of thread, other threads:[~2022-11-05  9:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-05  5:16 [Bug c++/107531] New: List of references calls destructors, add warning? nightstrike at gmail dot com
2022-11-05  5:20 ` [Bug c++/107531] " pinskia at gcc dot gnu.org
2022-11-05  5:27 ` nightstrike at gmail dot com
2022-11-05  9:12 ` redi at gcc dot gnu.org
2022-11-05  9:15 ` redi at gcc dot gnu.org
2022-11-05  9:29 ` 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).