It seems that boost::xpressive doesn't provide a lazily evaluated version of the new operator, so this semantic action won't compile:
using namespace boost::xpressive ;
std::vector<int*> vec ;
// Match any integer and capture into s1.
// Then use a semantic action to push it onto vec.
sregex num = (s1= +digit)[ ref(vec)->*push_back( new as<int>(s1) ) ] ;
Is there a construct for using the new operator in semantic actions? For example, boost::phoenix provides the new_ function for lambdas. Does xpressive provide something similar for semantic actions?
This is the best I've come up with so far. This code defines a function that allows you to use the syntax
new_<T>::with(...)as the semantic action equivalent tonew T(...). The function is modeled after the lazy function example in the boost xpressive user guide. (The snippet below only supports constructors with up to 2 parameters, but adding more is a matter of copy/paste.)And here it is in action:
Output:
To improve the generality of the code above, it may be better to change the parameter types to use
boost::add_reference<>andboost::add_const<>, but you get the idea.