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?