When should value parameters be destroyed?

Martin von Loewis loewis at informatik.hu-berlin.de
Fri Sep 29 08:59:49 UTC 2000


> Those of us present could not remember why destructors were included
> here, and decided it was probably a mistake, and intend to remove it
> (for both value parameters and results), if a request for rationale
> yields nothing.  Consider this message such a request.

Maybe that was an overlap in messages, but it appears that Daveed
already pointed to the right place: 5.2.2/4 explains the life time of
parameters. Mark's reference of 12.2/3 is not relevant - parameters
are not temporaries; instead, temporaries may be used to initialize
the parameters.

The critical point is that access checks must be made in the caller,
so

struct A{
private:
  ~A();
  friend void bar();
};

void foo(A a){
}

void bar(){
  A a;
  foo(a);
}

Since g++ invokes the destructor in the callee, it incorrectly rejects
this code:

a.cc: In function `void foo(A)':
a.cc:3: `A::~A()' is private
a.cc:7: within this context

Of course, the compiler could *still* invoke the destructor in the
callee under the as-if-rule, provided the access check occurs in the
caller.

That gets tricky since there are other conditions as well, e.g. that
destruction must occur outside a function-try-block of the callee - so
I'm in favour of having the caller destroy the object.

I have also concerns about the callee invoking the copy constructor,
if any. 8.5/12 states that parameter passing is copy-initialization,
and 12.8.15 gives permission to elide the call to the copy
constructor. Is this ABI giving the same permission to
implementations?

Consider

struct A{
  A(int);
  A(const A&);
  ~A();
};

void foo(A a){}

int getint();

void bar(){
  foo(getint());
}

In C++, two sequences of calls are possible:

1. getint, A(int), A(const A&), foo, ~A, ~A.
2. geting, A(int), foo, ~A.

It appears that the ABI mandates sequence A, since the copy ctor will
be called inside foo. I think this is not desirable; furthermore, the
same issue with access check and function-try-blocks arises.

So I propose to take 5.2.2 literal, and require parameter ctors and
dtors to be called *in* the caller.

Regards,
Martin




More information about the cxx-abi-dev mailing list