|
Boost : |
From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-09-29 15:11:09
With the caveat that I know next-to-nothing about the serialization
library, and thus am leaping into the discussion context-free, I do have
a quick comment.
On Mon, Sep 29, 2003 at 12:41:16PM -0700, Robert Ramey wrote:
> I've considered your example in more detail and now feel I can
> shed some light on it.
>
> suppose we have:
>
> class A
> {
> private:
> const m_i;
> A(); // make default unavailable
> public:
> A(int i) : m_i(i);
> };
>
> What does the following mean?
>
> boost::archive::text_iarchive oa;
> A a2(2);
> oa << a2;
>
> boost::archive::text_iarchive ia;
> A a1(1);
> ia >> a1;
>
> should the m_i member variable of a1 equal 2 or should it
> equal 1.
It always seemed to me that the flaw in using operator>> is that it
presumes the existence of an object. People write
Foo foo;
i >> foo;
to mean
unsigned char bits[ sizeof(Foo) ]; // suitably aligned
Foo& foo = *( (Foo*)( (void*)(bits) ) ); // cast the bits
i >> foo;
where the operator>> is defined as
... operator>>( ..., Foo& bits ) {
// get data from input stream
new (&bits) Foo(data); // placement new
...
}
When there is a default constructor, the operator>> works
(although inefficiently, as a needless dummy object is constructed and
then overwritten), but when there is no default constructor, the real
problems with the interface show up.
It seems to me that the right way to get a Foo out of a stream is not
Foo foo;
i >> foo;
but rather
Foo foo( i );
You construct a foo based on the data in the input stream.
I don't know anything about the design/interface constraints for the
library, so my comment maybe isn't applicable, but I just wanted to
interject the thought, as it seems to me to go to the core of the
problem being discussed.
As a final aside, would that we could write
uninitialized Foo foo; // new keyword
// call placement new to construct the object later
to mean
unsigned char bits[ sizeof(Foo) ]; // suitably aligned
Foo& foo = *( (Foo*)( (void*)(bits) ) ); // cast the bits
// call placement new to construct the object later
in C++! I hate having to "hack at the system level" to do something
which can be expressed more simply at the language level.
-- -Brian McNamara (lorgon_at_[hidden])
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk