Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2006-02-10 08:45:19


Fred Bertsch <fred.bertsch_at_[hidden]> writes:

> This is a fairly minor point, in my opinion, and I don't think I can
> say which way is better for shmem, but I'll try to explain it a bit
> better.
>
> For example, named_shared_object can only be created through its
> default constructor. Once created this way, you probably can't
> actually use your named_shared_object. Instead, you can complete the
> construction of the object by using open, create, or one of the other
> methods that "construct" the object. An error is indicated by the
> return value of these member functions. I don't actually know the name
> of this technique, and I suspect my point would be clearer if I knew
> its name.

If I'm understanding you correctly, that's known as "two-phase
construction" and it's generally a very bad idea, because it weakens
the class invariants and -- consequently -- the assumptions that code
can make about the object's state. It oftne means that the class has
a special "zombie" state that you always have to check for before
operating on it. The opposite of this technique is RAII.

> The alternative technique is to have several constructors or static
> member functions in named_shared_object. These would create an
> opened or created named_shared_object if nothing bad happened, and
> they would throw an exception if an error came up.
>
> I mostly brought this up because few things in boost use the first
> technique, so someone might have a strong opinion about it.

I'm surprised this hasn't drawn more fire. I'll be happy to condemn
it -- even before I've seen the details of the code -- and risk that
I've completely misinterpreted this in which case I'll have to
apologize.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk