Boost logo

Boost Users :

From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2007-05-23 02:33:53


Scott Meyers ha escrito:

> I'm interested in taking Andrei Alexandrescu's Ad Hoc Visitor technique
> (http://tinyurl.com/2k8h2h) and implementing it using the MPL. I need to
> implement a runtime function that iterates over an MPL type sequence until a
> dynamic_cast succeeds. Here's some pseudocode, where I use "typelist" to mean
> an MPL type sequence:
>
> class UnknownType { ... }; // for exceptions
>
> template <typename TL> // TL = "typelist"
> void visit(ASTNode *pn)
> {
> for (each type T in TL) { // try dynamic_
> if (T *pT = dynamic_cast<T*>(pn)) { // casting to all
> v.process(pT); // the types in TL
> return;
> }
>
> throw UnknownType(typeid(*pn)); // throw an ex. if
> } // no type matches
>
> I can make this work if I replace the for loop with a use of mpl::for_each, but
> for_each iterates over all elements of the type sequence, and I want to stop
> when a dynamic_cast succeeds. I can hack the function object I pass to for_each
> to have it be a no-op once a successful dynamic_cast has been encountered, but
> the operative word there is "hack."
>
> What I really want is something more like
> do_until<Sequence>(unaryFunctionObject) that will loop over all the entries in
> Sequence until the unaryFunctionObject returns true. Is there something like
> this in the MPL, must I roll my own, or am I approaching this the wrong way
> entirely?

AFAIK you must roll your own. Please find attached a possible implementation
using mpl::fold.

HTH,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net