Boost Phoenix (4)

Source: Internet
Author: User
Function

 

#include <boost/spirit/home/phoenix/function/function.hpp>

 

Functions in Phoenix are not boost generic function pointers. They are used to help you implement lazy functions. The main benefit of implementing a function as a lazy function is that it can coexist with other Phoenix modules in harmony. In this case, we will first press "no table" and explain it in detail later.

 

Operator

 

#include <boost/spirit/home/phoenix/operator.hpp>

 

This module reloads almost all the C ++ operators that can be reloaded. Of course they provide their lazy versions. In fact, it is because of these lazy operators that such code in the previous article is possible:

STD: generate (V. Begin (), V. End (), ref (I) ++)
STD: for_each (V. Begin (), V. End (), cout <arg1 <"");

Both ++ and <are parsed to the lazy version after the overload.

 

Statement

 

#include <boost/spirit/home/phoenix/statement.hpp>

 

This module is very interesting. It provides many c ++ statements, such as if, else, while, switch, and even try... catch lazy versions. It can be said that it is using C ++ to implement a lazy version of C ++!

In this case, it is abstract. Let's look at an example:

# Include <algorithm> <br/> # include <boost/spirit/home/phoenix/core. hpp> <br/> # include <boost/spirit/home/phoenix/operator. hpp> <br/> # include <boost/spirit/home/phoenix/bind. hpp> <br/> # include <boost/spirit/home/phoenix/statement. hpp> <br/> using namespace std; <br/> using namespace boost: phoenix: arg_names; <br/> struct Person <br/> {<br/> string name; <br/> uint Age; <br/> Person () {}< br/> Person (string const & _ name, uint _ age) <br/>: name (_ name) <br/>, age (_ age) <br/>{}< br/> void greet () <br/> {cout <"Hello, "<name <endl ;}< br/> void greet (string const & _ s) <br/> {cout <_ s <name <endl ;} <br/>}; <br/> int main () <br/>{< br/> typedef vector <Person> PersonList; <br/> PersonList pl; <br/> pl. push_back (Person ("Amy", 20); <br/> pl. push_ba Ck (Person ("Rose", 21); <br/> pl. push_back (Person ("Ralph", 30); <br/> pl. push_back (Person ("Zoe", 50); <br/> std: for_each (pl. begin (), pl. end (), <br/> if _ (bind (& Person: age, arg1) <25) <br/> [<br/> bind (& Person :: greet, arg1, "Hi, young") <br/>]. <br/> else _ <br/> [<br/> bind (& Person: greet, arg1) <br/>] <br/> ); <br/> std: for_each (pl. begin (), pl. end (), <br/> switch _ (bind (& Person: age, arg1) <Br/> [<br/> case _ <30> (cout <bind (& Person: name, arg1) <", you stand by yourself. "<endl), <br/> case _ <50> (cout <bind (& Person: name, arg1) <", you know your fate. "<endl), <br/> default _ (cout <bind (& Person: name, arg1) <", enjoy your life! "<Endl) <br/>] <br/>); <br/> return 0; <br/>}< br/> 

Output:

Hi, young Amy
Hi, young Rose
Hello, Ralph
Hello, Zoe
Amy, enjoy your life!
Rose, enjoy your life!
Ralph, you stand by yourself.
Zoe, you know your fate.

The use of IF _, else _, switch _, case _ greatly expands the use of for_each and implements some quite complex loops. We can imagine the power of combining the lazy statement with so many other STL algorithms.

 

 

Object

 

#include <boost/spirit/home/phoenix/object.hpp>

 

The object module consists of seven components, which correspond to the lazy versions of C ++ constructor, new, delete, const_cast, static_cast, dynamic_cast, and reinterprete_cast.

Let's look at an example. If we want to create a digital pyramid using a vector, how can we write it?

 

1
2 2
3 3 3
4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10

Of course, there is no problem in writing a double-layer loop. This is also a common exercise when you are learning programming. However, we can use lazy constructor of Phoenix to combine STL in just one sentence: # Include <iostream> <br/> # include <vector> <br/> # include <algorithm> <br/> # include <boost/spirit/home/phoenix/core. hpp> <br/> # include <boost/spirit/home/phoenix/operator. hpp> <br/> # include <boost/spirit/home/phoenix/object. hpp> <br/> # include <boost/spirit/home/phoenix/statement. hpp> <br/> # include <boost/spirit/home/phoenix/container. hpp> <br/> using namespace std; <br/> using namespace boost: phoenix: arg_names; <br/> int main () <br/>{< br/> typedef vector <int> Matrix; <br/> Matrix m (10 ); <br/> int I = 1; <br/> generate (m. begin (), m. end (), construct <vector <int> (ref (I) ++, ref (I); <br/> for_each (m. begin (), m. end (), <br/> (<br/> for _ (ref (I) = 0, ref (I) <size (arg1), ++ ref (I )) <br/> [<br/> cout <arg1 [ref (I)] <"<br/>], <br/> cout <val ("/n") <br/>); <br/> return 0; <br/>}< br/> 

 

In the above code, the code used to initialize matrix m into a pyramid has only one sentence:

Generate (M. Begin (), M. End (), construct <vector <int> (ref (I) ++, ref (I )));

The magic here is as follows:

The generate of STL will loop in the outermost layer of m. Every time construct <vector <int> (ref (I) ++, ref (I ))) construct <vector <int> constructs a vector on the stack. The parameters are the following two: ref (I) ++ and ref (I );
Ref (I) returns an auto-incrementing I every time. The first is the number of elements in the vector <int>, and the second is the initial value. So we get this digital pyramid, which is very concise. On the contrary, our printing code is quite long and lazy for statements are used. Is there any simple solution? Yes, but we have to wait until we finish talking about lazy container and lazy algorithm. Bind

Phoenix's BIND is similar to boost. Bind, but it is more robust. We have used it in the previous statement example. The reload function can be processed successfully. In the statement example, bind automatically selects the correct greet reload version based on the number of subsequent parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.