Re: How to create a shallow copy without calling a constructor?
On Jan 3, 10:54 am, viboes <vicente.bo...@wanadoo.fr> wrote:
Hello,
I need to create a cache of a transactional object without using the
new operator nor the copy constructor of the class.
Ok. (I have to do this myself sometimes, when interoperating with C
memory management functions is more important than correct design.)
This cache needs
only to copy the raw memory of the copied instance, is for that I have
called it shallow_clone
That's where our approaches start to diverge.
....
I have tried with
class C {
public:
C* shallow_clone() {
C* p = reinterpret_cast<C>(new char[sizeof(C)]);
Avoidable undefined behavior: instance of C exists in a potentially
not remotely valid state. Try
char* p = new char[sizeof(C)];
if (p==0) {
throw std::bad_alloc();
}
Unless you're using a compiler option that creates non-standard C++,
you don't need this null pointer test (new is guaranteed to throw
std::bad_alloc rather than return a null pointer).
new(std::no_throw) char[sizeof(C)] would require that test. [The
std::no_throw parameter means the new operator should return NULL
rather than throw std::bad_alloc()).]
std::memcpy(p, this, sizeof(C));
Ok.
return p;
reinterpret_cast goes here, as follows:
return reinterpret_cast<C*>(p);
}
};
But I suspect that this is not correct in general. Is this correct on
some particular cases? if yes on witch ones?
It is automatically operationally incorrect unless the specific
implementation, and hardware being targeted, makes it valid. I
wouldn't even try unless the class C was a C++0X standard-layout class
without virtual functions.
Fortunately, the reinterpret_cast is completely avoidable. See below.
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?
The most portable way I am aware of, uses careful type design and
inheritance to reduce the shallow copy to an operator= (which will be
operationally a std::memset).
The general idea is
struct x_struct
{
// data and member functions to be inherited; this must be a
standard-layout class at least, with no virtual functions. A POD-
struct is better.
// The derived class will want
void clear(); // XXX should be constructor; good way to leak
memory in other contexts
void destroy(); // XXX should be destructor;
};
// ACID; throws std::bad_alloc on failure
void value_copy(x_struct& dest, const x_struct& src);
// this is allowed to have virtual functions and other non-POD/non-
standard layout features
// it has no data members of its own.
class x_class : public x_struct
{
public:
x_class() {this->clear();};
x_class(const parse_tree_class& src)
{
this->clear();
value_copy(*this,src);
};
x_class(const parse_tree& src)
{
this->clear();
value_copy(*this,src);
};
virtual ~x_class() {this->destroy();};
const x_class& operator=(const x_class& src)
{
this->destroy();
value_copy(*this,src);
return *this;
}
const x_class& operator=(const x& src)
{
this->destroy();
value_copy(*this,src);
return *this;
}
};
If you don't have an API requirement for shallow_clone, ditch it as
worse than useless for code clarity.
// why bother, except for API consistency
x_struct* shallow_clone(const x_struct& src)
{
x_struct* p = new x_struct;
*p = src;
return p;
// or perhaps just this, using the default copy constructor?
// return new x_struct(src);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]