Mark P wrote:
Consider a class in which I redefine operator new(std::size_t):
struct A
{
void* operator new(std::size_t size) {/* my implementation */}
};
This has the consequence of hiding the placement version of operator new:
void* operator new(std::size_t size, void* location);
I can deal with this by defining placement new within A to call the
global placement new:
void* operator new(std::size_t size, void* location)
{
return ::operator new(size,loc);
}
Is this the recommended way to handle this situation? Is it possible to
achieve the same effect by some sort of using declaration?
Thanks,
Mark
_C++ Coding Standards_ by Sutter and Alexandrescu recommends in Item 46
that if you provide any class-specific new, provide all of the standard
forms (plain, placement, and nothrow). Otherwise, any form you don't
provide will be hidden from users of your class (e.g., the STL, which
uses placement new extensively).
If you have a base class that defines new operators, you can hoist them
into your derived class with "using", but you can't pull in names from
the global namespace (e.g. ::operator new) that way, so you'll have to
write forwarding functions to the global operators (as you did above).
Also, don't forget to supply a matching delete operator for each
non-placement new operator that you provide. Failing to do so is bad
(see _C++ Coding Standards_, Item 45).
Cheers! --M