Re: delete

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 06/01/04


Date: Tue, 01 Jun 2004 11:25:40 -0500

Jerry Coffin wrote:

>In article <es8kb0t3g5f1pmdp8cj9gfic5l9af1tt8q@4ax.com>, dsh@mvps.org
>says...
>
>[ creating a global "operator delete(void *&);" ]
>
>> It's illegal. The first parameter of operator delete must be void*.
>
>True -- that was why I said you "_might_" be able to do it, rather
>than that you could. Some older compilers accepted it (sorry, I
>don't remember which ones and I'm far too lazy to re-install every
>old compiler I have to figure out) but it's barely possible that the
>OP is using one that'll accept it even though the standard doesn't
>allow it.
>
>Even now, precise conformance with the C++ standard remains the
>exception rather than the rule...

Even VC6 (circa 1998) rejects that approach. In addition, you clipped the
part where I went on to explain that even if you used an old compiler that
allowed the overloading of operator delete on void*&, you'd still run into
ambiguities when you tried to use it:

*****

In addition, the following is ambiguous:

void f(void*);
void f(void*&);

void* p = 0;
f(p); // Ambiguous

However, this is fine, due to the strange interaction between overload
resolution and rvalues:

f((void*) p); // Selects f(void*)

*****

So there are multiple reasons why it doesn't work, and even if it did work
under some ancient compiler, imagine the effects of moving to a newer one.
Not only is your code immediately broken, you labored all that time with the
wrong understanding of the delete operator, or at best, you came to rely on
a crutch, which in the world of VC++, hasn't had a leg to stand on in at
least six years. Earlier, you did characterize it as a "poor idea", and I
elaborated on why it's an exceedingly poor idea, but in your reply, you seem
to have backed away from that. Bottom line: Even if it could be used, it
should not be used.

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)
  • Re: fields for methods?
    ... but only by a compiler that is allowed to ... struct A {void foo();}; ... int static_instance i = 0; ... Is not possible because foo is the only member of A... ...
    (comp.programming)
  • Re: Calling a matlab dll in java
    ... I compiled it with mcc to dll shared library ... all compiler and linker options in VS took me a while, ... JNIEXPORT void JNICALL Java_JNITest_JNITest_initializeApplication ... public void launchThread() ...
    (comp.soft-sys.matlab)
  • Re: Determining a functions calling address
    ... >> conversion between pointer to any type of function and pointer to ... >> void, or indeed pointer to any other object type. ... Apparently you are not using a real C++ compiler, ... First you should have received diagnostics along these lines: ...
    (comp.lang.cpp)
  • Re: Calling a matlab dll in java
    ... you mention about putting a -nojvm option when initializing the dll ... the matlab generated library inside my jni-dll) ... all compiler and linker options in VS took me a while, ... JNIEXPORT void JNICALL ...
    (comp.soft-sys.matlab)