Re: How to create a shallow copy without calling a constructor?
On Jan 5, 1:15 pm, viboes <vicente.bo...@wanadoo.fr> wrote:
On Jan 4, 4:19 pm, Ulrich Eckhardt <dooms...@knuut.de> wrote:
My context is Software Transactional Memory (STM). The shared
transactional objects need to be cached on transaction specific
objects. The pointers these transactional objects contains are
pointers to other transactional objects.
The idea I have in mind is that this cache don't need to do a deep
copy of the transactional object, but just a shallow copy, as the STM
system will take care of the copy of these pointee transactional
objects when modified. In addition I want the STM mechanism to
interfere as less as possible with the user space. That is why I don't
want to use copy constructor, assignment, neither class specific new/
delete operators.
Ok: you need a shallow copy that bypasses the main class invariants
for efficiency reasons.
The STM library I'm working on requires that any transactional object
inherits from
class base_transaction_object {
public:
virtual base_transaction_object*
make_cache(transaction* t) const = 0;
virtual void copy_cache(
base_transaction_object const * const) = 0;
virtual void delete_cache()=0;
virtual ~base_transaction_object() {};
...
};
I omit here why the STM library need this.
Don't have full control of the inheritance diagram: ok. Note that
this means std::memcpy close to automatically fails to make a valid
copy (you might get lucky on a very nice target).
IMO there is no need to call the destructor of Final. The reason is
that this object is not a real one, is a cache of other real object.
Yes: you're returning a doppleganger FinalCache object (which
hopefully has a trivial destructor) in place of a Final object.
Furthermore, this FinalCache object has to pretend to be a subclass of
base_transaction_object when it isn't.
I can't imagine how to implement this without invoking Undefined
Behavior all over the place, rather than just here. You will need a
zoo of test cases to verify that these changes work with your target
compiler (and verify that merely upgrading doesn't break things, let
alone changing compiler vendors).
For classes that have no trivial copy semantics we can yet do a
shallow copy, but as I understand it now, not without the help of
user. I'm wondering if it is worth to require the user to define the
following shallow functions:
struct shallow_t {};
const shallow_t shallow = {};
// Shallow copy constructor
C(C const&, shallow_t);
// Shallow assignement
C& shallow_assign(C const&);
This is reasonable, yes.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]