...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Lazy statements...
The expressions presented so far are sufficiently powerful to construct quite elaborate structures. We have presented lazy-functions and lazy-operators. How about lazy-statements? First, an appetizer:
Print all odd-numbered contents of an STL container using std::for_each
(all_odds.cpp):
std::for_each(c.begin(), c.end(), if_(arg1 % 2 == 1) [ cout << arg1 << ' ' ] );
Huh? Is that valid C++? Read on...
Yes, it is valid C++. The sample code above is as close as you can get to
the syntax of C++. This stylized C++ syntax differs from actual C++ code.
First, the if
has a trailing
underscore. Second, the block uses square brackets instead of the familiar
curly braces {}.
Note | |
---|---|
C++ in C++? In as much as Spirit attempts to mimic EBNF in C++, Phoenix attempts to mimic C++ in C++!!! |
Note | |
---|---|
Unlike lazy functions and lazy operators, lazy statements always return void. |
Here are more examples with annotations. The code almost speaks for itself.