re PR c++/20416 (Incorrect lifetime for temporary with static const reference)
PR c++/20416 * call.c (initialize_reference): Handle local static reference temps properly. From-SVN: r129020
This commit is contained in:
parent
37b22c8283
commit
42f02385bd
|
@ -1,3 +1,9 @@
|
|||
2007-10-04 Jason Merrill <jason@redhat.com>
|
||||
|
||||
PR c++/20416
|
||||
* call.c (initialize_reference): Handle local static reference
|
||||
temps properly.
|
||||
|
||||
2007-10-03 Jason Merrill <jason@redhat.com>
|
||||
|
||||
PR c++/32470
|
||||
|
|
|
@ -6824,6 +6824,10 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
|
|||
if (at_function_scope_p ())
|
||||
{
|
||||
add_decl_expr (var);
|
||||
|
||||
if (TREE_STATIC (var))
|
||||
init = add_stmt_to_compound (init, register_dtor_fn (var));
|
||||
else
|
||||
*cleanup = cxx_maybe_build_cleanup (var);
|
||||
|
||||
/* We must be careful to destroy the temporary only
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
// PR c++/20416. We correctly constructed the temporary S in foo(),
|
||||
// but incorrectly destroyed it every time foo() was called.
|
||||
// { dg-do run }
|
||||
extern "C" void abort (void);
|
||||
extern "C" void _exit (int);
|
||||
|
||||
int c, exiting;
|
||||
struct S {
|
||||
S() { ++c; }
|
||||
S(const S &) { ++c; }
|
||||
~S()
|
||||
{
|
||||
if (!exiting) abort ();
|
||||
_exit (0);
|
||||
}
|
||||
};
|
||||
void
|
||||
foo (void)
|
||||
{
|
||||
static const S &s = S();
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
if (c != 0)
|
||||
abort ();
|
||||
foo ();
|
||||
foo ();
|
||||
if (c != 1)
|
||||
abort ();
|
||||
exiting = 1;
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue