Topic: Does non-const operator take precedence


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sun, 5 Mar 1995 03:43:05 GMT
Raw View
dcb@lovat.fmrco.com (David Binderman) writes:

>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;
>>}
>
>Fergus Henderson writes:
>>I'd say it looks like Borland is wrong.
>>The non-const operator should be selected.
>
>I agree.
>
>>Your code is legal, IMHO.
>
>Two out of three compilers here say calling delete on a local object is not
>permitted.

They do?  Do they reject the above example?  What error message do they give?
What if I define

 A::operator char *() { return 0; }

?

It's true that calling the operator delete() function with the
address of a local variable would result in undefined behaviour.
But that's not happening in this example.  Simply using a local
object in a deallocation-expression (such as the `delete a' expression
above) IS permitted, I'm pretty sure.

>I agree with the two compilers.

Why?  Can you give some reference to the ARM or WP which would justify
rejecting the above example code?  I still think the code is legal.

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




Author: jason@cygnus.com (Jason Merrill)
Date: 03 Mar 1995 00:54:06 GMT
Raw View
>>>>> David Sachs <b91926@fsgi01.fnal.gov> writes:

> Ian T Zimmerman <itz@rahul.net> writes:
> ...
>> I think this begs the question. It could have been

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

> No, the code is correct. "delete a" is treated as "delete (char *)a",

This doesn't follow from

> since the conversion a ---> char* is well defined, and unique according
> to overloading rules.

this, which I agree with.  While a-->char* is well-defined and unique,
a-->"some pointer type" is not unique.

Jason




Author: Ian T Zimmerman <itz@rahul.net>
Date: 2 Mar 1995 04:12:59 GMT
Raw View
In article <1995Feb28.165147.1340@fmrco.uucp>,
David Binderman <dcb@lovat.fmrco.com> wrote:
>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;
>>}
>
>Fergus Henderson writes:
>>I'd say it looks like Borland is wrong.
>>The non-const operator should be selected.
>
>I agree.
>
>>Your code is legal, IMHO.
>
>Two out of three compilers here say calling delete on a local object is not
>permitted.
>
>I agree with the two compilers.
>

I think this begs the question. It could have been

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

cheers, itz.
--
Ian T Zimmerman            +-------------------------------------------+
P.O. Box 13445             I    With so many executioners available,   I
Berkeley, California 94712 I suicide is a really foolish thing to do.  I
USA  <itz@rahul.net>       +-------------------------------------------+




Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 2 Mar 1995 13:21:36 -0600
Raw View
Ian T Zimmerman <itz@rahul.net> writes:


...
>I think this begs the question. It could have been

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

No, the code is correct. "delete a" is treated as "delete (char *)a",
since the conversion a ---> char* is well defined, and unique according
to overloading rules.




Author: dcb@lovat.fmrco.com (David Binderman)
Date: Tue, 28 Feb 1995 16:51:47 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;
>}

Fergus Henderson writes:
>I'd say it looks like Borland is wrong.
>The non-const operator should be selected.

I agree.

>Your code is legal, IMHO.

Two out of three compilers here say calling delete on a local object is not
permitted.

I agree with the two compilers.

Regards

David C Binderman MSc BSc       +44 171 975 4723         dcb@lovat.fmrco.com
O-O Analysis & Design, C++, C, UNIX, telecomms, safety critical, compilers ...