Hi folks,
I am doing something Very Bad and Wrong (which I'll spare you the
details of) that requires overloading new for some specific classes.
So, for example:
class MyWeirdThingy
{
public:
...
static void* operator new(size_t size)
{
return my_weird_allocator(size);
}
};
This all works fine, there are no problems. However, I do also need to
use placement new for the same class in order to instantiate copies of
it contiguously in memory (as is typically the case with
implementations of std::vector, though I'm actually using my own
collection class).
If I attempt this with code like:
new (ptr) MyWeirdThingy(x)
to copy-construct an instance of the class to a particular address, gcc
wont compile the code because it fails to find an appropriate new
implementation. You can get around the error either by changing the
placement new to:
:: new (ptr) MyWeirdThingy(x)
or by adding a second overload within the class declaration of
MyWeirdThingy for placement new, e.g.:
static void* operator new(size_t size, void* p)
{
return p;
}
which is actually identical to the library version of placement ::new.
It all compiles and seems to work, but the copy construction doesn't
appear to be happening -- I seem to be seeing just default
construction. It is possible (very likely, actually!) that I have a
subtle bug somewhere, but I thought it worth asking whether it is
necessary to do anything beyond the above declaration when implementing
placement new -- since the same new gets called regardless of which
constructor is being invoked, surely you don't need to do anything
weird inside new in this case?
Confirmation that I'm not being stupid here would be appreciated.
Thank you in advance,
Sarah Thompson
PS: My reason for all this craziness is implementing a very unusual
memory model that supports something similar to transaction rollback
for a lattice of 'versions' -- it's part of an experimental model
checker.