Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Function template let
PrevUpHomeNext

Function template let

boost::xpressive::let — For binding local variables to placeholders in semantic actions when constructing a regex_iterator or a regex_token_iterator.

Synopsis

// In header: <boost/xpressive/regex_actions.hpp>


template<typename... ArgBindings> unspecified let(ArgBindings const &... args);

Description

xpressive::let() serves the same purpose as match_results::let(); that is, it binds a placeholder to a local value. The purpose is to allow a regex with semantic actions to be defined that refers to objects that do not yet exist. Rather than referring directly to an object, a semantic action can refer to a placeholder, and the value of the placeholder can be specified later with a let expression. The let expression created with let() is passed to the constructor of either regex_iterator or regex_token_iterator.

See the section "Referring to Non-Local Variables" in the Users' Guide for more discussion.

Example:

// Define a placeholder for a map object:
placeholder<std::map<std::string, int> > _map;

// Match a word and an integer, separated by =>,
// and then stuff the result into a std::map<>
sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
    [ _map[s1] = as<int>(s2) ];

// The string to parse
std::string str("aaa=>1 bbb=>23 ccc=>456");

// Here is the actual map to fill in:
std::map<std::string, int> result;

// Create a regex_iterator to find all the matches
sregex_iterator it(str.begin(), str.end(), pair, let(_map=result));
sregex_iterator end;

// step through all the matches, and fill in
// the result map
while(it != end)
    ++it;

std::cout << result["aaa"] << '\n';
std::cout << result["bbb"] << '\n';
std::cout << result["ccc"] << '\n';

The above code displays:

1
23
456

Parameters:

args

A set of argument bindings, where each argument binding is an assignment expression, the left hand side of which must be an instance of placeholder for some X, and the right hand side is an lvalue of type X.


PrevUpHomeNext