1// phoenix_style_control_structures.cpp -- The Boost Lambda Library ------
2//
3// Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
5//
6// Distributed under the Boost Software License, Version 1.0. (See
7// accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9//
10// For more information, see www.boost.org
11
12// -----------------------------------------------------------------------
13
14
15#include <boost/core/lightweight_test.hpp>
16#define BOOST_CHECK BOOST_TEST
17
18#include "boost/lambda/lambda.hpp"
19#include "boost/lambda/if.hpp"
20#include "boost/lambda/loops.hpp"
21
22#include <iostream>
23#include <vector>
24#include <list>
25#include <algorithm>
26#include <cmath>
27#include <cassert>
28#include <functional>
29
30
31
32using namespace boost::lambda;
33using namespace std;
34
35
36
37// If-else, while, do-while, for statements
38
39
40int main() {
41
42 vector<int> v;
43 v.clear();
44 v.push_back(x: 1);
45 v.push_back(x: 2);
46 v.push_back(x: 3);
47 v.push_back(x: 4);
48 v.push_back(x: 5);
49 v.push_back(x: 6);
50 v.push_back(x: 7);
51 v.push_back(x: 8);
52 v.push_back(x: 9);
53 v.push_back(x: 10);
54
55 int sum = 0;
56 //////////////////////////////////
57 for_each(first: v.begin(), last: v.end(),
58 f: if_(cond: _1 > 3 && _1 <= 8)
59 [
60 sum += _1
61 ]
62 );
63
64 BOOST_CHECK(sum == 4+5+6+7+8);
65
66 int gt = 0, eq = 0, lt = 0;
67 //////////////////////////////////
68 for_each(first: v.begin(), last: v.end(),
69 f: if_(cond: _1 > 5)
70 [
71 ++var(t&: gt)
72 ]
73 .else_
74 [
75 if_(cond: _1 == 5)
76 [
77 ++var(t&: eq)
78 ]
79 .else_
80 [
81 ++var(t&: lt)
82 ]
83 ]
84 );
85
86 BOOST_CHECK(lt==4);
87 BOOST_CHECK(eq==1);
88 BOOST_CHECK(gt==5);
89
90 vector<int> t = v;
91
92 int counta = 0;
93 int countb = 0;
94 //////////////////////////////////
95 for_each(first: v.begin(), last: v.end(),
96 f: (
97 while_(cond: _1--)
98 [
99 ++var(t&: counta)
100 ],
101 ++var(t&: countb)
102 )
103 );
104
105 BOOST_CHECK(counta == 55);
106 BOOST_CHECK(countb == 10);
107
108
109 v = t;
110
111 counta = 0; countb = 0;
112 //////////////////////////////////
113 for_each(first: v.begin(), last: v.end(),
114 f: (
115 do_
116 [
117 ++var(t&: counta)
118 ]
119 .while_(cond: _1--),
120 ++var(t&: countb)
121 )
122 );
123
124 BOOST_CHECK(counta == (2+11)*10/2);
125 BOOST_CHECK(countb == 10);
126
127
128 v = t;
129 counta = 0; countb = 0;
130 //////////////////////////////////
131 int iii;
132 for_each(first: v.begin(), last: v.end(),
133 f: (
134 for_(init: var(t&: iii) = 0, cond: var(t&: iii) < _1, step: ++var(t&: iii))
135 [
136 ++var(t&: counta)
137 ],
138 ++var(t&: countb)
139 )
140 );
141
142 BOOST_CHECK(counta == (1+10)*10/2);
143 BOOST_CHECK(countb == 10);
144
145 v = t;
146
147 return boost::report_errors();
148}
149

source code of boost/libs/lambda/test/phoenix_control_structures.cpp