Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2000-08-31 11:20:46


> >* ref<T> is a polymorphic value type, i.e. you can use op-> on it to
access
> >T's virtual member functions.
>
> So ref<t> assumes T is a base, but otherwise supports the copying etc
> semantics? This contrasts with any which just works on values and
> assumes no conversion relationship, so if you know exactly what T is
> then to_ptr()->m works fine, but won't work if a hierarchy is assumed.

Yes. ref<T> holds its value by a T*, so it can hold anything derived from T,
or anything at all when T is void.

> So does that make ref<t> more like a regular smart pointer type?

It's similar to a smart pointer, yes... but it is a value type at the same
time. :)

> >* it has operator== and operator< to support std::sort and similar
> >algorithms.
>
> any makes few assumptions about its containee, and although I have a
> version that has these, I am not entirely happy with it: either you
> require == and < supported on all containee types, or you have a traits
> class that allows the programmer to provide reasonable behaviour (or
> non-behaviour) as necessary.

ref<T> makes no assumptions about operator== and <. It doesn't work in all
cases, but is close.

Hmm. I think I'll just upload an example so you can take a look yourself.

> >* it has its own RTTI system and doesn't use (or require) type_info.
>
> Was this for portability, or...?

It's a side effect of the implementation. I don't like unnessary couplings,
so I decided not to pull in <typeinfo>.

> >* It compiles on our all time favorite compiler.
>
> Is that a benefit or...? ;-)

Depends. Have a look at the code. :)

> Separate from any, I have also played around with a type that supports a
> greater range of operations and conversions. In the docs I classified
> variant types in one of three categories, with any qualifying as a
> "discriminated type w/o conversions". A type that supports extra
> operations and the like is in the "converting type" category. I think it
> is worth having such a creature, but I think it's a different type.

ref<> is not a converting creature. It's the thing you use when you want a
std::vector (or std::set) of polymorphic objects.

--
Peter Dimov
Multi Media Ltd.

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