### Include Header for Boost Phoenix Lazy Prelude Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/tutorial_with_examples This code snippet includes the necessary header file for using the Boost Phoenix lazy prelude functions. It is a prerequisite for the provided examples. ```c++ #include ``` -------------------------------- ### Boost.Phoenix try_catch Statement Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/try__catch__statement Provides a concrete example of the try_catch statement in Boost.Phoenix, demonstrating its application for handling different exception types. The example shows how to catch `runtime_error`, a general `exception` (accessing its `what()` message via a bound variable `e_`), and any other unhandled exceptions using `catch_all`. ```cpp try_ [ f(arg1) ] .catch_() [ cout << val("caught runtime error or derived\n") ] .catch_(e_) [ cout << val("caught exception or derived: ") << bind(&exception::what, e_) << val("\n") ] .catch_all [ cout << val("caught some other type of exception\n") ] ``` -------------------------------- ### Using Declarations for Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/tutorial_with_examples These C++ using declarations bring specific elements from the Boost Phoenix library into the current scope. They are required for the tutorial examples to function correctly. ```c++ using boost::phoenix::arg_names::arg1; using boost::phoenix::arg_names::arg2; using namespace boost::phoenix; ``` -------------------------------- ### Main Function Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/define_expression Demonstrates the usage of the custom 'plus' generator within the main function. It shows how to create 'plus' expressions, display them using proto::display_expr, and evaluate them by calling the expression object. This example highlights the basic functionality of custom expressions in Boost Phoenix. ```cpp #include #include #include namespace phoenix = boost::phoenix; namespace proto = boost::proto; // define the expression namespace expression { template struct plus : phoenix::expr {}; } // extend the grammar, to recognice the expression namespace boost { namespace phoenix { template <> struct meta_grammar::case_ : enable_rule< ::expression::plus< meta_grammar , meta_grammar > > {}; }} // build a generator template typename expression::plus::type plus(Lhs const & lhs, Rhs const & rhs) { return expression::plus::make(lhs, rhs); } int main() { plus(6, 5); proto::display_expr(plus(6, 5)); std::cout << plus(5, 6)() << "\n"; } ``` -------------------------------- ### C++ Boost Phoenix val() Function Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/values This C++ code snippet demonstrates the basic usage of the `boost::phoenix::val` function. It creates function objects that evaluate to constant integer and string values. The example requires the Boost Phoenix core library. Output is printed to standard output. ```cpp #include #include int main() { using boost::phoenix::val; std::cout << val(3)() << std::endl; std::cout << val("Hello World")() << std::endl; return 0; } ``` -------------------------------- ### Demonstrate Expression Inversion in Main Function using Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/invert The 'main' function in this C++ code demonstrates the 'print_expr' and 'invert' functionalities. It uses Boost Phoenix placeholders like '_1', '_2', etc., to construct various arithmetic and conditional expressions. Each expression is passed to 'print_expr' to show the effect of the inversion rules, and one example directly uses the 'invert' function. ```cpp int main() { using phoenix::placeholders::_1; using phoenix::placeholders::_2; using phoenix::placeholders::_3; using phoenix::placeholders::_4; print_expr(_1); print_expr(_1 + _2); print_expr(_1 + _2 - _3); print_expr(_1 * _2); print_expr(_1 * _2 / _3); print_expr(_1 * _2 + _3); print_expr(_1 * _2 - _3); print_expr(if_(_1 * _4)[_2 - _3]); print_expr(_1 * invert(_2 - _3)); } ``` -------------------------------- ### Boost.Phoenix do_while Statement Syntax and Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/___do_while_____statement Demonstrates the syntax of the do_while_ statement in Boost.Phoenix, which allows for do-while loop constructs within Phoenix expressions. It requires the header. The example shows how to use it with std::for_each to print elements followed by a newline. ```c++ #include #include #include #include int main() { std::vector c = {1, 2, 3, 4, 5}; int count = 0; std::cout << "Output: " << std::endl; std::for_each(c.begin(), c.end(), ( // The sequenced_statement part starts here boost::phoenix::cout << boost::phoenix::arg(1) << ", " ) .while_( // The conditional_expression part starts here boost::phoenix::arg(1)-- > 0 ) ); std::cout << boost::phoenix::val("\nLoop finished.") << std::endl; return 0; } ``` -------------------------------- ### Evaluating a Phoenix Expression (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/inside/expression Demonstrates how to evaluate a Phoenix expression created using the `plus` generator function. The example shows calling the expression directly `plus(6, 5)()` to get the result and using `proto::display_expr` to print its structure. ```cpp plus(6, 5)(); proto::display_expr(plus(5, 6)); ``` -------------------------------- ### Boost Phoenix Value - C++ Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/core/values Demonstrates the implicit and explicit creation of value expressions in Boost Phoenix, and how to evaluate them. It includes the necessary header and shows the use of `val()` for constants within expressions. ```cpp #include #include int main() { // Implicit creation of value expression // add(arg1, 6) implicitly creates expression::value::type for 6 // Explicit creation using val() auto explicit_val_int = boost::phoenix::val(6); auto explicit_val_string = boost::phoenix::val("Hello World"); // Evaluating a value std::cout << boost::phoenix::val(3)() << " " << boost::phoenix::val("Hello World")() << std::endl; return 0; } ``` -------------------------------- ### Lazy String Construction Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/object/construction Demonstrates constructing a std::string object lazily using the construct template with two arguments. This example shows how construct() defers the actual string instantiation until evaluation time. ```cpp construct(arg1, arg2) ``` -------------------------------- ### Boost Phoenix for_ Statement Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/for_statement An example demonstrating the Boost Phoenix for_ statement to print each element of a container N times, where N is the element's value. It utilizes `std::for_each` and shows how `for_` can be used as an expression passed to STL algorithms. ```cpp int iii; std::for_each(c.begin(), c.end(), ( for_(ref(iii) = 0, ref(iii) < arg1, ++ref(iii)) [ cout << arg1 << ", " ], cout << val("\n") ) ); ``` -------------------------------- ### Take First N Elements from Lazy List with take() Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/tutorial_with_examples/list_generation Extracts the first N elements from a lazy list generator. In this example, it retrieves the first 4 integers from an infinite sequence starting at 1, returning [1, 2, 3, 4]. The resulting list remains lazy and is only fully evaluated when its members are accessed. ```cpp take(4,enum_from(1)) ``` -------------------------------- ### C++ Boost Phoenix 'val' Demonstration Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/callback This C++ code snippet illustrates the fundamental use of `boost::phoenix::val`. The `val` construct wraps a literal value, making it callable like a function. The `print` helper function takes a callable object (like one created by `val`) and executes it, printing its result to the standard output. This example shows `val` being used with both an integer and a string literal. ```cpp #include #include template void print(F f) { std::cout << f() << std::endl; } int main() { using boost::phoenix::val; print(val(3)); print(val("Hello World")); return 0; } ``` -------------------------------- ### Access Element by Zero-Based Index with at() Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/tutorial_with_examples/list_generation Retrieves a specific element from a lazy list using zero-based indexing. This example accesses the element at index 3 (fourth element) from the infinite sequence starting at 1, returning the value 4. The function only evaluates the list up to the requested index. ```cpp at(enum_from(1),3) ``` -------------------------------- ### Using Boost Phoenix Argument Placeholders (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/arguments This C++ code snippet demonstrates how to use Boost Phoenix's `arg_names::arg1` and `arg_names::arg2` to access the first and second arguments passed to a placeholder, respectively. It requires the `` and `` headers. The output shows the values retrieved using these placeholders. ```cpp #include #include int main() { using boost::phoenix::arg_names::arg1; using boost::phoenix::arg_names::arg2; int i = 3; char const* s = "Hello World"; std::cout << arg1(i) << std::endl; // prints 3 std::cout << arg2(i, s) << std::endl; // prints "Hello World" return 0; } ``` -------------------------------- ### Define a Mutable Function (Pseudo Code) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/core/references Illustrates a pseudo-code example of a function `add_assign` that modifies its first parameter, demonstrating the need for mutable arguments. ```c++ void add_assign(T& x, T y) { x += y; } // pseudo code ``` -------------------------------- ### Phoenix Expression Tree Template - Placeholder and Arithmetic Operations Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/inside/actions Shows a simple Phoenix expression template demonstrating how placeholders (`_1`, `_2`) and arithmetic operations are combined to form an expression tree. This expression uses multiplication and addition operations with placeholders representing input parameters, which gets evaluated as an AST with specific rules and actions. ```C++ _1 + 3 * _2 ``` -------------------------------- ### Complete Lazy Evaluation Example with Variable Arguments Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/basics Illustrates both stages of Phoenix lazy evaluation by showing partial application followed by final evaluation. Variables are passed to the partially applied expression, demonstrating how the modulo operator evaluates to boolean results (1 for true, 0 for false). ```cpp int x = 1; int y = 2; std::cout << (arg1 % 2 == 1)(x) << std::endl; // prints 1 or true std::cout << (arg1 % 2 == 1)(y) << std::endl; // prints 0 or false ``` -------------------------------- ### Lazy Member Function Binding Examples Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/bind/binding_member_functions Multiple examples demonstrating various patterns of binding member functions with Boost Phoenix, including lazy binding of arguments and objects. Shows how both the object and arguments can be lazily evaluated. ```cpp xyz obj; bind(&xyz::foo, arg1, arg2) // arg1.foo(arg2) bind(&xyz::foo, obj, arg1) // obj.foo(arg1) bind(&xyz::foo, obj, 100) // obj.foo(100) ``` -------------------------------- ### Evaluating a Reference to Get its Value Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/core/references This example shows how to evaluate a reference created by `ref()` by invoking it like a function `()`. This retrieves the current value of the referenced variable. ```c++ int i = 3; char const* s = "Hello World"; cout << ref(i)() << ref(s)(); ``` -------------------------------- ### Boost.Phoenix Argument Evaluation (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/core/arguments Shows how to evaluate Boost.Phoenix arguments to retrieve specific arguments passed to a function. Examples demonstrate retrieving the first and second arguments using arg1 and arg2. ```C++ #include #include // ... assuming add is defined elsewhere ... char c = 'A'; int i = 123; const char* s = "Hello World"; // Assuming arg1 and arg2 are defined as placeholders // cout << arg1(c) << endl; // Get the 1st argument: c // cout << arg1(i, s) << endl; // Get the 1st argument: i // cout << arg2(i, s) << endl; // Get the 2nd argument: s // Expected Output: // A // 123 // Hello World ``` -------------------------------- ### Example Usage of Phoenix Container Size Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/container_actor Demonstrates how to use the Boost Phoenix STL container extensions in a 'main' function. It creates a std::vector, initializes it, and then uses the 'container(arg1).size()' and 'con1.size()' expressions to print the vector's size, comparing it with the direct call to 'v.size()'. ```cpp int main() { container_actor::type> const con1; std::vector v; v.push_back(0); v.push_back(1); v.push_back(2); v.push_back(3); std::cout << (container(arg1).size())(v) << " == " << v.size() << "\n"; std::cout << (con1.size())(v) << " == " << v.size() << "\n"; } ``` -------------------------------- ### Define Invert Actions for Arithmetic Operations using Boost Phoenix and Proto Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/invert This C++ code defines a custom evaluation rule set 'invert_actions' using Boost Phoenix and Proto. It overrides specific grammar rules like 'plus', 'minus', 'multiplies', and 'divides' to perform inverse operations. For example, addition is transformed into subtraction. The code includes conditional compilation for MSVC compatibility. ```cpp #include #include #include namespace phoenix = boost::phoenix; namespace proto = boost::proto; using phoenix::evaluator; #ifdef _MSC_VER // redifining evaluator, this is because MSVC chokes on function types like: // F(G(...)) #define evaluator(A0, A1) proto::call #endif struct invert_actions { template struct when : proto::nary_expr< proto::_ , proto::vararg< proto::when > > {}; }; template <> struct invert_actions::when : proto::call< phoenix::functional::make_minus( evaluator(proto::_left, phoenix::_context) , evaluator(proto::_right, phoenix::_context) ) > {}; template <> struct invert_actions::when : proto::call< phoenix::functional::make_plus( evaluator(proto::_left, phoenix::_context) , evaluator(proto::_right, phoenix::_context) ) > {}; template <> struct invert_actions::when : proto::call< phoenix::functional::make_divides( evaluator(proto::_left, phoenix::_context) , evaluator(proto::_right, phoenix::_context) ) > {}; template <> struct invert_actions::when : proto::call< phoenix::functional::make_multiplies( evaluator(proto::_left, phoenix::_context) , evaluator(proto::_right, phoenix::_context) ) > {}; #ifdef _MSC_VER #undef evaluator #endif ``` -------------------------------- ### Print and Invert Expressions with Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/invert This C++ code provides two utility functions: 'print_expr' and 'invert'. 'print_expr' takes an expression, displays its original form, and then its form after applying the 'invert_actions' rules. The 'invert' function directly applies the inversion evaluation to an expression using a context created with 'invert_actions'. ```cpp template void print_expr(Expr const & expr) { std::cout << "before inversion:\n"; proto::display_expr(expr); std::cout << "after inversion:\n"; proto::display_expr( phoenix::eval( expr , phoenix::context( phoenix::nothing , invert_actions() ) ) ); std::cout << "\n"; } template typename boost::phoenix::result_of::eval< Expr const& , phoenix::result_of::make_context< phoenix::result_of::make_env<>::type , invert_actions >::type >::type invert(Expr const & expr) { return phoenix::eval( expr , phoenix::make_context( phoenix::make_env() , invert_actions() ) ); } ``` -------------------------------- ### Create and invoke reference wrappers with Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/references Uses boost::phoenix::ref() to create callable reference wrappers around integer and string variables. The ref() function wraps the variable and allows it to be invoked with the () operator to retrieve its value. This example demonstrates basic Phoenix reference semantics with simple type variables. ```cpp #include #include int main() { using boost::phoenix::ref; int i = 3; char const* s = "Hello World"; std::cout << ref(i)() << std::endl; std::cout << ref(s)() << std::endl; return 0; } ``` -------------------------------- ### Boost Phoenix if_else_ Statement Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/___if_else_____statement Demonstrates the usage of the Boost Phoenix if_else_ statement with nested conditions. This example iterates through a container and prints messages based on the value of each element. ```c++ std::for_each(c.begin(), c.end(), if_(arg1 > 5) [ cout << arg1 << " > 5\n" ] .else_ [ if_(arg1 == 5) [ cout << arg1 << " == 5\n" ] .else_ [ cout << arg1 << " < 5\n" ] ] ); ``` -------------------------------- ### Define and Use Factorial Function with Boost Phoenix (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/factorial This snippet defines a custom factorial function using Boost Phoenix's function object and then demonstrates its usage. It shows how to instantiate the function and call it directly or with argument placeholders. Dependencies include Boost Phoenix core and function headers. ```cpp #include #include #include #include #include struct factorial_impl { template struct result; template struct result : result {}; template struct result { typedef Arg type; }; template Arg operator()(Arg n) const { return (n <= 0) ? 1 : n * this->operator()(n-1); } }; int main() { using boost::phoenix::arg_names::arg1; boost::phoenix::function factorial; int i = 4; std::cout << factorial(i)() << std::endl; std::cout << factorial(arg1)(i) << std::endl; return 0; } ``` -------------------------------- ### Define Function for Binding in C++ Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/bind/binding_functions Define a simple C++ function that will be bound using Boost Phoenix bind. This example shows a void function that takes an integer parameter and outputs it to standard output. ```cpp void foo(int n) { std::cout << n << std::endl; } ``` -------------------------------- ### Standard Library Copy Algorithm with Iterators Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/stl/algorithm Shows the traditional standard library approach to copying arrays using std::copy with explicit iterator pairs. This requires specifying both the start and end iterators for the source array. ```cpp int array[] = {1, 2, 3}; int output[3]; std::copy(array, array + 3, output); // We have to provide iterators // to both the start and end of array ``` -------------------------------- ### List Generation Functions in Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/what_is_provided Lazy functions for generating lists in boost::phoenix. Provides enumeration from a starting point, enumeration within a range, and list construction from multiple elements. ```cpp boost::phoenix::enum_from boost::phoenix::enum_from_to boost::phoenix::list_with ``` -------------------------------- ### Deferred Operator Execution Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/operator Illustrates how operator expressions are deferred. `(arg1 + arg2) * arg3` initially returns an actor without performing calculations. A subsequent function call, like printing its result, triggers the actual operator evaluation. ```cpp #include #include // ... Assuming arg1, arg2, arg3 are phoenix actors ... // std::cout << ((arg1 + arg2) * arg3)(4, 5, 6); ``` -------------------------------- ### Lazy Operator Expressions in Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/starter_kit/lazy_operators Basic examples of forming lazy expressions using Boost Phoenix operators. Shows arithmetic, assignment, and indexing operations with Phoenix primitives and expressions. At least one operand in binary expressions must be a Phoenix primitive or expression for lazy evaluation. ```cpp arg1 * arg1 ref(x) = arg1 + ref(z) arg1 = arg2 + (3 * arg3) ref(x) = arg1[arg2] // assuming arg1 is indexable and arg2 is a valid index ``` -------------------------------- ### Boost Phoenix throw_ Statement C++ Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/throw_ Demonstrates the usage of the Boost Phoenix throw_ statement for throwing and re-throwing exceptions. It includes examples of throwing specific exception types and re-throwing caught exceptions within a try/catch block. Requires the boost/phoenix/statement/throw.hpp header. ```cpp #include try_ [ f(arg1) ] .catch_() [ cout << val("caught runtime error or derived\n"), throw_() ] .catch_() [ cout << val("caught exception or derived\n"), throw_() ] .catch_all [ cout << val("caught some other type of exception\n"), throw_(runtime_error("translated exception")) ] ``` -------------------------------- ### Find First Odd Number using Boost.Phoenix in C++ Source: https://www.boost.org/doc/libs/latest/libs/phoenix/example/find_if This C++ code snippet finds the first odd number in a std::vector using Boost.Phoenix's functional objects. It relies on the ``, ``, and ``, `` headers. The input is a std::vector of integers, and the output is the first odd number found, printed to the console. ```C++ #include #include #include #include #include int main() { using boost::phoenix::arg_names::arg1; int init[] = { 2, 10, 4, 5, 1, 6, 8, 3, 9, 7 }; std::vector c(init, init + 10); typedef std::vector::iterator iterator; // Find the first odd number in container c iterator it = std::find_if(c.begin(), c.end(), arg1 % 2 == 1); if (it != c.end()) std::cout << *it << std::endl; // if found, print the result return 0; } ``` -------------------------------- ### Include Boost Phoenix Algorithm Module Headers Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/stl/algorithm Demonstrates how to include the main algorithm header and category-specific headers for iteration, transformation, and querying algorithms. The main header provides access to all algorithm wrappers. ```cpp #include #include #include #include ``` -------------------------------- ### Boost C++ Libraries: Object Construction, Creation, Deletion, and Casting Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/starter_kit/construct__new__delete__casts Demonstrates the usage of Boost C++ Libraries for object construction, creation of new objects, deletion of objects, and C++ style casts. These functions often append a trailing underscore to avoid conflicts with C++ reserved keywords. They operate on object types and pointers, facilitating memory management and type conversions. ```c++ construct(arg1, arg2) // constructs a std::string from arg1, arg2 new_(arg1, arg2) // makes a new std::string from arg1, arg2 delete_(arg1) // deletes arg1 (assumed to be a pointer) static_cast_(arg1) // static_cast's arg1 to an int* ``` -------------------------------- ### Decrement Elements with while_ Loop Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/while__statement Demonstrates using while_ with std::for_each to decrement each container element until it reaches zero, printing values at each iteration. The example uses Phoenix's lazy evaluation to create a functional loop construct. ```cpp std::for_each(c.begin(), c.end(), ( while_(arg1--) [ cout << arg1 << ", " ], cout << val("\n") ) ); ``` -------------------------------- ### Basic Lambda Expression Syntax in Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/scope/lambda Demonstrates the fundamental syntax for creating lambda expressions in Boost Phoenix, consisting of the lambda keyword followed by a body enclosed in square brackets. ```cpp lambda [ lambda-body ] ``` -------------------------------- ### Include Boost Phoenix Construct Header Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/object/construction Include the necessary Boost Phoenix header file to access lazy constructor functionality. This header provides the construct template and related utilities for deferred object creation. ```cpp #include ``` -------------------------------- ### Include Boost Phoenix let Header Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/scope/let Include the header file required to use the `let` construct from the Boost Phoenix library. ```cpp #include ``` -------------------------------- ### Boost Phoenix Argument Functions Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/starter_kit/arguments Demonstrates the usage of predefined argument functions in Boost.Phoenix, such as arg1, arg2, and arg3, which return their respective positional arguments when called. These functions are fundamental for creating functional objects that operate on input parameters. ```cpp arg1 // one-or-more argument function that returns its first argument arg2 // two-or-more argument function that returns its second argument arg3 // three-or-more argument function that returns its third argument ``` ```cpp int i = 3; char const* s = "Hello World"; std::cout << arg1(i) << std::endl; // prints 3 std::cout << arg2(i, s) << std::endl; // prints "Hello World" ``` -------------------------------- ### Boost Block Statement Error Example Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/block_statement Illustrates an incorrect usage of Boost Block Statements where a trailing comma is placed after the last statement, which is an error. ```plaintext statement, statement, statement, // ERROR! ``` -------------------------------- ### Implement Identity Function with Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/implementation_details Implements an identity function `id()` using Boost.Phoenix, which simply returns its input argument. This example showcases the structure for defining a Phoenix function, including result type deduction. ```cpp namespace impl { struct Id { template struct result; template struct result : boost::remove_reference {}; template A0 operator()(A0 const & a0) const { return a0; } }; } typedef boost::phoenix::function Id; Id id; ``` -------------------------------- ### Phoenix call Template Wrapper for Proto Transforms Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/inside/actions Defines the `call` template wrapper provided by Phoenix as a convenience abstraction over Proto's `proto::call`. This template accepts a function object type and automatically handles calling it with the rule's matched expression children and the evaluation context, simplifying the creation of custom actions for AST evaluation. ```C++ template struct call; ``` -------------------------------- ### Basic Addition with Phoenix plus() Function Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/tutorial_with_examples/arithmetic_functions Demonstrates multiple ways to use the plus() function with Phoenix placeholders (arg1, arg2) for lazy arithmetic evaluation. The function can be partially applied with different combinations of arguments and placeholders, and invoked immediately with () or deferred until all values are provided. ```cpp int a = 123; int b = 256; plus(arg1, arg2)(a, b) plus(arg1, b)(a) plus(a, arg2)(a,b) plus(a, arg1)(b) plus(a, b)() ``` -------------------------------- ### Phoenix Function with Defined Return Type (Zero Arguments) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/lazy_list/implementation_details Demonstrates defining a Boost.Phoenix function with a specified return type and no arguments. This example uses a templated struct to manage the return type and provides a default value. ```cpp namespace impl { template struct what0 { typedef Result result_type; Result operator()() const { return Result(100); } }; } typedef boost::function0 fun0_int; boost::function0 what0_int = impl::what0(); typedef boost::phoenix::function What0_arg; What0_arg what0_arg(what0_int); ``` -------------------------------- ### Invoke Bound Function with Arguments Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/bind/binding_functions Evaluate a bound function expression by providing the required arguments through a function call invocation. This demonstrates calling the bound function with a specific value that gets passed to the original function. ```cpp bind(&foo, arg1)(4); ``` -------------------------------- ### Phoenix Algorithm Copy with Range Interface Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/stl/algorithm Demonstrates the Phoenix algorithm module's range-based approach to copying arrays. Only two arguments are required as the end of the range is determined automatically by the Boost.Range library. ```cpp int array[] = {1, 2, 3}; int output[3]; copy(arg1, arg2)(array, output); // Notice only 2 arguments, the end of // array is established automatically ``` -------------------------------- ### Boost Block Statements in std::for_each Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/statement/block_statement Provides an example of using Boost Block Statements within the std::for_each algorithm. The comma operator chain within parentheses is used to group multiple operations. ```cpp std::for_each(c.begin(), c.end(), ( do_this(arg1), do_that(arg1) ) ); ``` -------------------------------- ### Partial Application with Modulo and Equality Operators Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/basics Demonstrates the first stage of lazy evaluation in Phoenix using partial function application. The expression creates a higher-order function that can be invoked later with actual arguments, showing how the modulo operator and equality comparison are applied without immediate evaluation. ```cpp arg1 % 2 == 1 ``` -------------------------------- ### Lazy Function with State-Aware Constructor Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/function This C++ example illustrates how to construct a lazy function (`boost::phoenix::function`) by passing an instance of the underlying polymorphic function object (`factorial_impl`), which can be useful for functions requiring state. ```cpp boost::phoenix::function factorial(ftor); ``` -------------------------------- ### Boost.Phoenix Predefined Arguments (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/core/arguments Demonstrates the definition of predefined Boost.Phoenix arguments, including both the 'argN' and '_N' styles, which represent the Nth function argument. ```C++ #include namespace placeholders { expression::argument<1>::type const arg1 = {}; expression::argument<2>::type const arg2 = {}; expression::argument<3>::type const arg3 = {}; } namespace placeholders { expression::argument<1>::type const _1 = {}; expression::argument<2>::type const _2 = {}; expression::argument<3>::type const _3 = {}; } ``` -------------------------------- ### Include Boost Phoenix Tuple Module Header Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/stl/tuple Include the Boost Phoenix tuple module header to enable lazy tuple operations and unpacking placeholders. This module requires C++14 or higher. ```cpp #include ``` -------------------------------- ### Include Boost Phoenix Lambda Header Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/scope/lambda Includes the Boost Phoenix lambda library header file to enable lambda expression functionality in C++ code. ```cpp #include ``` -------------------------------- ### Using Placeholder for Partial Function Application in STL `find_if` Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/basics Demonstrates how Boost.Phoenix allows for partial function application within STL algorithms like `std::find_if`. The `arg1` placeholder represents an argument to be supplied later, creating a lambda function with an arity of 1. This lambda is then used by `find_if` to filter elements based on a condition. ```cpp std::find_if(c.begin(), c.end(), arg1 % 2 == 1) ``` -------------------------------- ### Use Lambda to Print Container Elements in C++ Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/scope/lambda Practical example using lazy for_each with lambda to print all elements from a container. Demonstrates how lambda-body arguments refer to values passed by std::for_each, distinct from outer scope arguments. ```cpp for_each(arg1, lambda[cout << arg1]) ``` -------------------------------- ### Declare and Use References in C++ Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/starter_kit/references Demonstrates how to declare and use references to variables in C++. References allow functions to hold a reference to a value stored elsewhere. The `ref()` function is used to create these references. ```cpp int i = 3; char const* s = "Hello World"; ref(i) ref(s) ``` -------------------------------- ### Define Struct with Member Function for Binding Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/bind/binding_member_functions Example struct definition containing a const member function that can be bound using Boost Phoenix. This demonstrates the typical structure of a class whose member functions will be used with the bind mechanism. ```cpp struct xyz { void foo(int) const; }; ``` -------------------------------- ### Include Boost Phoenix Core Reference Header Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/core/references This code snippet shows the necessary include directive to use the core reference functionalities from the Boost Phoenix library. ```c++ #include ``` -------------------------------- ### Using a Lazy Function with STL Algorithms in C++ Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/starter_kit/lazy_functions This example shows how to use a custom lazy function, `is_odd`, with the STL algorithm `std::find_if`. The lazy function is invoked with `arg1`, indicating it will operate on an argument passed by `std::find_if`, enabling functional-style programming with standard algorithms. ```cpp std::find_if(c.begin(), c.end(), is_odd(arg1)); ``` -------------------------------- ### Static Cast Address with Lazy Cast in Boost Phoenix Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/modules/object/casts Example of using static_cast_ to lazily cast an address of an argument to a base class pointer type. The expression static_cast_(&arg1) defers the cast operation until the lambda is evaluated, enabling functional composition. ```cpp static_cast_(&arg1) ``` -------------------------------- ### Boost.Phoenix For Loop Grammar Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/inside/rules Defines the grammar rule for a for loop in Boost.Phoenix. It includes initialization (Init), condition (Cond), step (Step), and do-block (Do). ```C++ rule::for_ : expression::for_( meta_grammar , meta_grammar , meta_grammar , meta_grammar ) ``` -------------------------------- ### Defining Phoenix Expression Templates (C++) Source: https://www.boost.org/doc/libs/latest/libs/phoenix/doc/html/phoenix/inside/expression Defines the base template `expr_ext` for extending Proto Expressions and a convenience template `expr` for creating Phoenix expressions. `expr_ext` takes an Actor template, a Tag, and child types, while `expr` defaults to using the `actor` template. ```cpp template