Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-12-09 16:08:44


From: "Yuval Ronen" <ronen_yuval_at_[hidden]>
>
> > > 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 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>();

But you can't. You have to write the following, as I showed
above:

   int a = some_any.template 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?

Do you still like your version?

Yes, I think cast fits quite nicely, but I hate forcing the use
of the "template" qualifier in function calls when I can avoid
it.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

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