Boost logo

Boost :

From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2004-12-09 03:48:21


> > Wouldn't it be nicer if class any will have a member method like:
> >
> > template <typename T>
> > T get() const;
> >
> > or maybe even:
> >
> > template <typename T>
> > const T& get() const;
> > template <typename T>
> > T& get();
>
> So you'd prefer writing this:
>
> boost::any<...> a(...);
> int i(a.template get<int>());
>
> to this?
>
> boost::any<...> a(...);
> int i(any_cast<int>(a));
>
> The latter has the advantage of looking just like static_cast et
> al, avoiding the ugly template qualification of the member
> function invocation, and being shorter.
>
> I think the current approach is better.

I blieve this a matter of point of view. I think of it as "extracting the
inner object" within the any object, so get() sounds more intuitive. I'd
much rather writing something like:
    int a = some_any.get<int>();
You think of it as a cast so any_cast sounds better to you. My guess that
neither of us will be able to convince the other, so I'll suggest some sort
of compromise: why don't we have both?

The get() method(s) will be used by weird people like me who want to
"extract the value/reference of the inner object", while the any_cast will
be used by all the rest. If you'll implement the any_cast using the new
get() method(s), you'll get some more benefits, such as removing the 'friend
any_cast' and possibly removing the const_cast from one of the any_cast<>s.

It's just my opinion,
Yuval


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