1/*=============================================================================
2 Copyright (c) 2001-2007 Joel de Guzman
3 Copyright (c) 2015 John Fletcher
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7==============================================================================*/
8#include <iostream>
9#include <cmath>
10#include <algorithm>
11#include <vector>
12
13#include <boost/phoenix/core/limits.hpp>
14
15#include <boost/detail/lightweight_test.hpp>
16#include <boost/fusion/tuple.hpp>
17#include <boost/phoenix/core.hpp>
18#include <boost/phoenix/operator.hpp>
19#include <boost/phoenix/function.hpp>
20#include <boost/phoenix/fusion.hpp>
21#include <boost/phoenix/scope.hpp>
22
23#include <typeinfo>
24
25namespace fusion = boost::fusion;
26namespace mpl = boost::mpl;
27
28int
29main()
30{
31 using boost::phoenix::let;
32 using boost::phoenix::val;
33 using boost::phoenix::arg_names::_1;
34 using boost::phoenix::arg_names::_2;
35 using boost::phoenix::local_names::_a;
36 using boost::phoenix::local_names::_b;
37
38 {
39 // show that we can return a local from an outer scope
40 int y = 0;
41#ifdef __OPTIMIZE__
42 int x = (let(_a = 1)[let(_b = _1)[ _a + 0 ]])(y);
43#else
44 int x = (let(a0: _a = 1)[let(a0: _b = _1)[ _a ]])(y);
45#endif
46 BOOST_TEST(x == 1);
47 }
48 {
49 // show that we can return a local from an inner scope
50 int y = 1;
51 int x = (let(a0: _a = 0)[let(a0: _b = _1)[ _b ]])(y);
52
53 BOOST_TEST(x == 1);
54 }
55 {
56 // show that we can return a local from an outer scope
57 //int y = 0;
58#ifdef __OPTIMIZE__
59 int x = (let(_a = 1)[let(_b = _a)[ _a + 0 ]])();
60#else
61 int x = (let(a0: _a = 1)[let(a0: _b = _a)[ _a ]])();
62#endif
63 BOOST_TEST(x == 1);
64 }
65 {
66 // show that we can return a local from an inner scope
67 //int y = 0;
68#ifdef __OPTIMIZE__
69 int x = (let(_a = 1)[let(_b = _a)[ _b + 0 ]])();
70#else
71 int x = (let(a0: _a = 1)[let(a0: _b = _a)[ _b ]])();
72#endif
73 BOOST_TEST(x == 1);
74 }
75 {
76 // show that we can return a local from an outer scope
77 int y = 1;
78 int x = (let(a0: _a = _1)[let(a0: _b = _a)[ _a ]])(y);
79
80 BOOST_TEST(x == 1);
81 }
82 {
83 // show that we can return a local from an inner scope
84 int y = 1;
85 int x = (let(a0: _a = _1)[let(a0: _b = _a)[ _b ]])(y);
86
87 BOOST_TEST(x == 1);
88 }
89
90 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++
91 // Be very careful. Some of these cases give a silly answer
92 // with clang 3.4 with C++03 and work for C++11.
93 // gcc 4.8.2 seems O.K. both ways. Oh dear.
94 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++
95 {
96 int y = 0;
97#ifdef __OPTIMIZE__
98 int x = (let(_a = 1, _b = 2)[let(_b = _a)[ _a + 0 ]])(y);
99#else
100 int x = (let(a0: _a = 1, a1: _b = 2)[let(a0: _b = _a)[ _a ]])(y);
101#endif
102 //std::cout << x << " P1A "; //clang - empty memory
103 BOOST_TEST(x == 1);
104 }
105 {
106 int y = 0;
107#ifdef __OPTIMIZE__
108 int x = (let(_a = 1, _b = 2)[let(_b = _a)[ _b + 0 ]])(y);
109#else
110 int x = (let(a0: _a = 1, a1: _b = 2)[let(a0: _b = _a)[ _b ]])(y);
111#endif
112 //std::cout << x << " P1B "; //clang - 42 value- one step better
113 BOOST_TEST(x == 1);
114 }
115 {
116 int y = 0;
117#ifdef __OPTIMIZE__
118 int x = (let(_a = val(1), _b = val(2))[let(_b = _a)[ val(_a) ]])(y);
119#else
120 int x = (let(a0: _a = val(t: 1), a1: _b = val(t: 2))[let(a0: _b = _a)[ _a ]])(y);
121#endif
122 //std::cout << x << " P2A "; //clang - 42 value - one step better
123 BOOST_TEST(x == 1);
124 }
125 {
126 int y = 0;
127#ifdef __OPTIMIZE__
128 int x = (let(_a = val(1), _b = val(2))[let(_b = _a)[ val(_b) ]])(y);
129#else
130 int x = (let(a0: _a = val(t: 1), a1: _b = val(t: 2))[let(a0: _b = _a)[ _b ]])(y);
131#endif
132 //std::cout << x << " P2B "; //clang - 42 value - one step better
133 BOOST_TEST(x == 1);
134 }
135 {
136 int y = 1;
137 int x = (let(a0: _a = _1, a1: _b = val(t: 2))[let(a0: _b = _a)[ _a ]])(y);
138 //std::cout << x << " P3 "; //clang - OK - one step better still
139 BOOST_TEST(x == 1);
140 }
141
142 {
143 int y = 0;
144#ifdef __OPTIMIZE__
145 int x = (let(_a = 1, _b = 2)[let(_b = _1)[ _a + 0 ]])(y);
146#else
147 int x = (let(a0: _a = 1, a1: _b = 2)[let(a0: _b = _1)[ _a ]])(y);
148#endif
149 // std::cout << x << " Q "; // clang 4201472
150 BOOST_TEST(x == 1);
151 }
152
153
154 return boost::report_errors();
155}
156
157

source code of boost/libs/phoenix/test/scope/more_let_tests.cpp