|
Boost : |
Subject: [boost] How to create a shallow copy without calling a constructor?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-01-02 10:42:45
Hello,
While developing Boost.STM (Software Transactional Memory), I need to create a cache of a transactional object without using the new operator nor the copy constructor of the class. This cache needs only to copy the raw memory of the copied instance, is for that I have called it shallow_clone
The following will create a deep copy and don't respect my requirements
class C {
public:
C* shallow_clone() {
return new C(*this);
}
};
I have looked at uninitialized_copy but if I have understood it correctly, it calls the copy constructor.
I have tried with
class C {
public:
C* shallow_clone() {
C* p = reinterpret_cast<C>(malloc(sizeof(C));
if (p==0) {
throw std::bad_alloc();
}
memcpy(p, this, sizeof(C));
return p;
}
};
But I suspect that this is not correct in general. Is this correct on some particular cases? if yes on witch ones?
Is there a way to create such a cache instance without calling to the constructor in a portable way using some low level C++ interface?
Thanks,
Vicente
_____________________
Vicente Juan Botet Escribá
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk