Topic: Does non-const operator take precedence over const operator?


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sun, 12 Mar 1995 23:35:37 GMT
Raw View
tony@online.tmx.com.au (Tony Cook) writes:

>Ian Johnston (johnston@caiman.enet.dec.com) wrote:
>
>: So how are you supposed to delete/destroy an object which *has* been
>: created via placement new?
>
>Destroy using:
> A *p = ....;
>
> p->A::~A();

Unless you have some good reason for suppressing the virtual behaviour of
virtual destructors, you should use

 p->~A();

That way, your code will still work if the initialization was

 class B : public A { ... };
 A *p = new B;

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sun, 5 Mar 1995 14:26:28 GMT
Raw View
johnston@caiman.enet.dec.com (Ian Johnston) writes:

>jason@cygnus.com (Jason Merrill) writes:
>
>>  5.3.5  Delete                                            [expr.delete]
>>
>>2 In  either  alternative,  if the value of the operand of delete is the
>>  null pointer the operation has no effect.   Otherwise,  in  the  first
>>  alternative  (delete object), the value of the operand of delete shall
>>  be a pointer to a non-array object created by a new-expression without
>                                                   ^^^^^^^^^^^^^^^^^^^^^^
>>  a   new-placement   specification,   or  a  pointer  to  a  sub-object
>   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>  (_intro.memory_)  representing  a  base  class  of  such   an   object
>>  (_class.derived_).
>
>So how are you supposed to delete/destroy an object which *has* been
>created via placement new?

You destroy it using an explicit destructor call.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: Fri, 3 Mar 1995 06:20:19 GMT
Raw View
In article <MECKLEN.95Feb27164200@oops.cs.utah.edu>,
Robert Mecklenburg <mecklen@oops.cs.utah.edu> wrote:
>Borland C++ 4.5 selects the const operator in the following code.  I
>have other compilers which do not.  Who is right?
>
>struct A
>{
>    operator const char *() const;
>    operator char *();
>};
>void foo()
>{
>    A a;
>    delete a;
>}

Borland WAS right.  Delete required a pointer to const argument.
But the rule has just been changed.
--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189




Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: Fri, 3 Mar 1995 06:25:15 GMT
Raw View
In article <MECKLEN.95Feb27164200@oops.cs.utah.edu>,
Robert Mecklenburg <mecklen@oops.cs.utah.edu> wrote:
>Borland C++ 4.5 selects the const operator in the following code.  I
>have other compilers which do not.  Who is right?
>
>struct A
>{
>    operator const char *() const;
>    operator char *();
>};
>void foo()
>{
>    A a;
>    delete a;
>}

 WOOPS. Ignore my last reply, I got it backwards.
delete required a pointer to _non-const_ argument, so the
non-const operator should have been called. Deleting
a pointer to const has just been approved. In that case,
it depends on whether the signature of delete is:

 // overloaded
 delete const T*
 delete T*

or

 delete T*

In the first case, your code becomes ambiguous. In the second,
the non-const operator would be called. But it depends even
further on the rules for binding argumentys to parameters
of psuedo templates -- can a cv-qualified type be "deduced" here?

Can of worms.  I try to avoid conversion operators.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189




Author: tony@online.tmx.com.au (Tony Cook)
Date: Sun, 5 Mar 1995 06:14:54 GMT
Raw View
Ian Johnston (johnston@caiman.enet.dec.com) wrote:

: So how are you supposed to delete/destroy an object which *has* been
: created via placement new?

Destroy using:
 A *p = ....;

 p->A::~A();

how you release the memory is up to you.

--
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com




Author: jason@cygnus.com (Jason Merrill)
Date: 28 Feb 1995 06:39:54 GMT
Raw View
>>>>> Robert Mecklenburg <mecklen@oops.cs.utah.edu> writes:

> Borland C++ 4.5 selects the const operator in the following code.  I
> have other compilers which do not.  Who is right?

> struct A
> {
>     operator const char *() const;
>     operator char *();
> };
> void foo()
> {
>     A a;
>     delete a;
> }

Looks ambiguous to me.

  5.3.5  Delete                                            [expr.delete]

2 In  either  alternative,  if the value of the operand of delete is the
  null pointer the operation has no effect.   Otherwise,  in  the  first
  alternative  (delete object), the value of the operand of delete shall
  be a pointer to a non-array object created by a new-expression without
  a   new-placement   specification,   or  a  pointer  to  a  sub-object
  (_intro.memory_)  representing  a  base  class  of  such   an   object
  (_class.derived_).

  +-------                     BEGIN BOX 31                     -------+
  Issue: ... or a class with an unambiguous conversion to such a pointer
  type ...
  +-------                      END BOX 31                      -------+

Jason




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Tue, 28 Feb 1995 14:50:34 GMT
Raw View
mecklen@oops.cs.utah.edu (Robert Mecklenburg) writes:

>Borland C++ 4.5 selects the const operator in the following code.  I
>have other compilers which do not.  Who is right?
>
>struct A
>{
>    operator const char *() const;
>    operator char *();
>};
>void foo()
>{
>    A a;
>    delete a;
>}

I'd say it looks like Borland is wrong.  The non-const operator
should be selected.  Your code is legal, IMHO.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au
all [L] (programming_language(L), L \= "Mercury") => better("Mercury", L) ;-)




Author: johnston@caiman.enet.dec.com (Ian Johnston)
Date: 1 Mar 1995 12:57:49 GMT
Raw View
In article <JASON.95Feb27223954@phydeaux.cygnus.com>, jason@cygnus.com
(Jason Merrill) writes:

[snip]

>  5.3.5  Delete                                            [expr.delete]
>
>2 In  either  alternative,  if the value of the operand of delete is the
>  null pointer the operation has no effect.   Otherwise,  in  the  first
>  alternative  (delete object), the value of the operand of delete shall
>  be a pointer to a non-array object created by a new-expression without
                                                   ^^^^^^^^^^^^^^^^^^^^^^
>  a   new-placement   specification,   or  a  pointer  to  a  sub-object
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>  (_intro.memory_)  representing  a  base  class  of  such   an   object
>  (_class.derived_).
>
>  +-------                     BEGIN BOX 31                     -------+
>  Issue: ... or a class with an unambiguous conversion to such a pointer
>  type ...
>  +-------                      END BOX 31                      -------+
>
>Jason
>

So how are you supposed to delete/destroy an object which *has* been
created via placement new?

--
Consulting for Digital Equipment Corp  johnston@caiman.enet.dec.com
      (33) 92.95.51.74




Author: mecklen@oops.cs.utah.edu (Robert Mecklenburg)
Date: 27 Feb 1995 23:42:00 GMT
Raw View
Borland C++ 4.5 selects the const operator in the following code.  I
have other compilers which do not.  Who is right?

struct A
{
    operator const char *() const;
    operator char *();
};
void foo()
{
    A a;
    delete a;
}

Thanks,
Robert Mecklenburg
University of Utah
http://www.cs.utah.edu/~mecklen