Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2000-09-04 05:40:26


> >Having copy_to as a free function makes it easier for somebody to make it
> >work with, say, ref<void> instead of any.
>
> Well, there's nothing preventing anyone from wrapping it :-)

Wrapping should not be necessary. The point is that to_ptr is sufficient;
copy_to and any_cast can be implemented in terms of to_ptr. Consider:

template<class U> bool copy_to(any const & from, U & to)
{
    if(U * p = from.to_ptr<U>())
    {
        to = *p;
        return true;
    }
    else
    {
        return false;
    }
}

template<class T, class U> bool copy_to_aux(T const & from, U & to,
true_type)
{
    to = from;
    return true;
}

template<class T, class U> bool copy_to_aux(T const &, U &, false_type)
{
    return false;
}

template<class T, class U> bool copy_to(T const & from, U & to)
{
    return copy_to_aux(from, to, is_convertible_to<T, U>::eval);
}

This makes copy_to even more generic.

> I would prefer not to provide a hacked interface with deprecated
> features if possible (and I believe it is), and instead provide an
> interface that is a proper supertype of the full one.

I don't quite understand what are you objecting to.

Proper compilers would eat

a.to_ptr<T>();

and all compilers, including MSVC, would compile the alternative

a.to_ptr((T*)0);

It is a deprecated feature, granted, but this is the lesser evil.

> >So when you std::sort a vector of ref's, the contents are sorted by type,
> >then by value.
>
> Err, "subtle". This mixes two ordering concepts.

No, this is a strict weak ordering on the <type, value> pair. How else would
you make std::set<ref> work?

> >However, you can't do anything generic with an 'any', regardless of
> >whether you have its type_info or not, except getting its name() (which
is
> >useless anyway.)
>
> I would argue that it's not useless, and in simple command line
> interpreters turns out to be quite useful!

The standard doesn't guarantee the usefulness of type_info::name(). It's
useful if you know what your compiler does, but it's not portable.

> >That's why I asked for an example that clearly demonstrates that the
ability
> >to ask an 'any' for its type_info is a part of the minimal interface. So,
> >what are those things that need runtime binding? :)
>
> map<const std::type_info *, handler, type_info_before> handler_table;

Why didn't you say so before? :)

Yes, this is the example I came up with myself.

Background: I'm trying to decide whether ref<> needs type_info() or not.
This example does prove the need for any::type_info. However, ref doesn't
need this sort of dispatch because you have access to the virtual functions
of the base type.

--
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