Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2000-09-01 11:20:04


> >if(U * p = rx.is<U>())

> >if(rx.is<U>())

> Although source-level compatible, I think the naming may be a little
> misleading, but YMMV.

It is. to_ptr<U> is better in the first case, while is<U> is better in the
second.

> any var;
> ...
> std::string text;
> var.copy_to(text);

[versus]

>
> if(std::string * converted = var.to_ptr<std::string>(var))
> text = *converted;
> else
> ...

Yes, I see. So copy_to() can be implemented in terms of the public interface
of 'any' (to_ptr). Wouldn't this make it a candidate for a free function?

> >(2) typical type_info() usage.
>
> The following allows a simple sorting wrt type, and verification of
> result:
>
> bool type_info_ordering(const any & lhs, const any & rhs)
> {
> return lhs.type_info().before(rhs.type_info());
> }
>
> void print_type_info(const any & to_print)
> {
> cout << to_print.type_info().name() << endl;
> }
>
> void sort_example()
> {
> vector<any> many;
> ... // set up with values
> sort(many.begin(), many.end(), type_info_ordering);
> for_each(many.begin(), many.end(), print_type_info);
> }
>
> The following demonstrates matching/filtering:
>
> struct is_type : binary_function<any, const type_info *, bool>
> {
> bool operator()(
> const any & operand, const type_info * type) const
> {
> return operand.type_info() == *type;
> }
> };
>
> void find_example()
> {
> vector<any> many;
> ...
> vector<any>::iterator first_int =
> find_if(
> many.begin(), many.end(),
> bind2nd(is_type(), &typeid(int)));
> ...
> }

But note that the first example is already supported by ref<> (its op<
compares the types of the two objects) and the second can be easily done
with ref::is<int>, or any::to_ptr<int>. I was thinking of an example that
demonstrates the need for the type_info() function. Having a minimal
interface is good, isn't it?

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