Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2006-12-12 17:49:54


AMDG

"Korcan Hussein" wrote:
> I propose 3 changes/additions made to boost::any:
>
> 1. Support for custom allocator types.
>
Do you intend to make boost::any a typedef then?
> 2. Support for in-place factories.
>
I think that would be nice.
> 3. Last but not least boost::variant style visitors.
>
> I'll elaborate on the last one, i've written primarily support for
> boost::variant style visitors for boost::any. The method is based on the
> acyclic visitor pattern and works as follows:
>
> struct printer_fn : boost::dynamic_visitor<int, std::string> {
>
> void operator()(int) const { std::cout << "int\n" }
> void operator(const std::string&) cosnt { std::cout << "string\n" }
>
> } const printer;
>
> ......
>
> boost::any a = 1;
>
> boost::apply_visitor(a, printer);
>
> a = std::string();
>
> boost::apply_visitor(a, printer);
>
Meaning boost::apply_visitor(printer, a) I assume?
> I'm looking to also provide the ability to extend their visitors (and
> override if need be) to support more types as well as provide "catch-all"
> (i.e (const) boost::any&) implementation which defaults to throwing an
> exception. I also want this to be constant-correct in the way of
> const_iterator and iterator, i need to look into this carefully since there
> are 4-cases to deal with (instead of 2).
>
Do you mean

struct deref_print :
boost::dynamic_visitor<std::vector<int>::const_iterator> {
    void operator()(std::vector<int>::const_iterator iter) const {
       std::cout << *iter;
    }
};

std::vector<int> v(1);
boost::any a = v.begin();
boost::apply_visitor(deref_print(), a);

> Is there any interest for these changes/additions?
>
> Thanks.
> Korcan Hussein.

For this last I don't think there is a good non-intrusive solution.
If the default is to throw an exception then it effectively limits the
contained type to those enumerated. In this case it is probably
better to use variant in the first place.

In Christ,
Steven Watanabe


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