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 
25 namespace fusion = boost::fusion;
26 namespace mpl = boost::mpl;
27 
28 int
main()29 main()
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 #if defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 50000) && __OPTIMIZE__
42         int x = (let(_a = _2)[let(_b = _1)[ _a ]])(y,1);
43 #else
44         int x = (let(_a = 1)[let(_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(_a = 0)[let(_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 #if defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 50000) && __OPTIMIZE__
59         int x = (let(_a = 1)[let(_b = _a)[ _a ]])();
60 #else
61         int x = (let(_a = _1)[let(_b = _a)[ _a ]])(1);
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 #if defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 50000) && __OPTIMIZE__
69         int x = (let(_a = _1)[let(_b = _a)[ _b ]])(1);
70 #else
71         int x = (let(_a = 1)[let(_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(_a = _1)[let(_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(_a = _1)[let(_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     int x = (let(_a = 1, _b = 2)[let(_b = _a)[ _a ]])(y);
98     //std::cout << x << " P1A "; //clang - empty memory
99         BOOST_TEST(x == 1);
100   }
101   {
102     int y = 0;
103     int x = (let(_a = 1, _b = 2)[let(_b = _a)[ _b ]])(y);
104     //std::cout << x << " P1B "; //clang - 42 value- one step better
105         BOOST_TEST(x == 1);
106   }
107   {
108     int y = 0;
109     int x = (let(_a = val(1), _b = val(2))[let(_b = _a)[ _a ]])(y);
110     //std::cout << x << " P2A "; //clang - 42 value - one step better
111         BOOST_TEST(x == 1);
112   }
113   {
114     int y = 0;
115     int x = (let(_a = val(1), _b = val(2))[let(_b = _a)[ _b ]])(y);
116     //std::cout << x << " P2B "; //clang - 42 value - one step better
117         BOOST_TEST(x == 1);
118   }
119   {
120     int y = 1;
121     int x = (let(_a = _1, _b = val(2))[let(_b = _a)[ _a ]])(y);
122     //std::cout << x << " P3 "; //clang - OK - one step better still
123         BOOST_TEST(x == 1);
124   }
125 
126   {
127     int y = 0;
128     int x = (let(_a = 1, _b = 2)[let(_b = _1)[ _a ]])(y);
129     //    std::cout << x << " Q "; // clang 4201472
130         BOOST_TEST(x == 1);
131   }
132     */
133 
134     return boost::report_errors();
135 }
136 
137