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