<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
-<link rel="up" href="../proto.html" title="Chapter 31. Boost.Proto">
-<link rel="prev" href="../proto.html" title="Chapter 31. Boost.Proto">
+<link rel="up" href="../proto.html" title="Chapter 32. Boost.Proto">
+<link rel="prev" href="../proto.html" title="Chapter 32. Boost.Proto">
<link rel="next" href="reference.html" title="Reference">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
The following table summarizes the discussion above:
</p>
<div class="table">
-<a name="boost_proto.users_guide.getting_started.naming.t0"></a><p class="title"><b>Table 31.1. Proto Naming Conventions</b></p>
+<a name="boost_proto.users_guide.getting_started.naming.t0"></a><p class="title"><b>Table 32.1. Proto Naming Conventions</b></p>
<div class="table-contents"><table class="table" summary="Proto Naming Conventions">
<colgroup>
<col>
<span class="identifier">i</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span>
</pre>
<p>
- There is also a <code class="computeroutput"><a class="link" href="../boost/proto/lit_idp927766608.html" title="Function lit">proto::lit()</a></code> function for constructing
+ There is also a <code class="computeroutput"><a class="link" href="../boost/proto/lit_idp701561072.html" title="Function lit">proto::lit()</a></code> function for constructing
a <code class="computeroutput"><a class="link" href="../boost/proto/literal.html" title="Struct template literal">proto::literal<></a></code> in-place. The above
expression can simply be written as:
</p>
In the type computation above, <code class="computeroutput"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">as_child</span><span class="special"><></span></code> is a metafunction that ensures
its argument is a Proto expression type. If it isn't one already, it
becomes a Proto terminal. We'll learn more about this metafunction, along
- with <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>, its runtime counterpart,
+ with <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>, its runtime counterpart,
<a class="link" href="users_guide.html#boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child" title="Controlling How Child Expressions Are Captured">later</a>.
For now, you can forget about it.
</p></td></tr>
<span class="identifier">proto</span><span class="special">::</span><span class="identifier">lit</span><span class="special">(</span><span class="number">1</span><span class="special">)(</span><span class="number">2</span><span class="special">)(</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">)(</span><span class="number">5</span><span class="special">,</span><span class="number">6</span><span class="special">,</span><span class="number">7</span><span class="special">,</span><span class="number">8</span><span class="special">);</span>
</pre>
<p>
- That may look strange at first. It creates an integer terminal with <code class="computeroutput"><a class="link" href="../boost/proto/lit_idp927766608.html" title="Function lit">proto::lit()</a></code>, and then invokes it like
+ That may look strange at first. It creates an integer terminal with <code class="computeroutput"><a class="link" href="../boost/proto/lit_idp701561072.html" title="Function lit">proto::lit()</a></code>, and then invokes it like
a function again and again. What does it mean? Who knows?! You get to decide
when you define an evaluation context or a transform. But more on that
later.
<p>
We could write a <code class="computeroutput"><span class="identifier">pow</span><span class="special">()</span></code>
function using code like this, but it's verbose and error prone; it's too
- easy to introduce subtle bugs by forgetting to call <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>
+ easy to introduce subtle bugs by forgetting to call <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>
where necessary, resulting in code that seems to work but sometimes doesn't.
- Proto provides a better way to construct expression nodes: <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code>.
+ Proto provides a better way to construct expression nodes: <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code>.
</p>
<h6>
<a name="boost_proto.users_guide.front_end.making_lazy_functions.h1"></a>
Functions Made Simple With <code class="literal">make_expr()</code></a>
</h6>
<p>
- Proto provides a helper for building expression templates called <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code>. We can concisely define
+ Proto provides a helper for building expression templates called <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code>. We can concisely define
the <code class="computeroutput"><span class="identifier">pow</span><span class="special">()</span></code>
function with it as below.
</p>
<p>
Subsequent template parameters to <code class="computeroutput"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">make_expr</span><span class="special"><></span></code> represent child nodes. If a child
type is not already a Proto expression, it is automatically made into a
- terminal with <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>.
+ terminal with <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>.
A type such as <code class="computeroutput"><span class="identifier">pow_fun</span><span class="special"><</span><span class="identifier">Exp</span><span class="special">></span></code> results in terminal that is held by
value, whereas a type like <code class="computeroutput"><span class="identifier">Arg</span>
<span class="keyword">const</span> <span class="special">&</span></code>
(note the reference) indicates that the result should be held by reference.
</p>
<p>
- In the function body is the runtime invocation of <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code>.
- It closely mirrors the return type calculation. <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code>
+ In the function body is the runtime invocation of <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code>.
+ It closely mirrors the return type calculation. <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code>
requires you to specify the node's tag type as a template parameter. The
arguments to the function become the node's children. When a child should
be stored by value, nothing special needs to be done. When a child should
be stored by reference, you must use the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span><span class="special">()</span></code> function to wrap the argument.
</p>
<p>
- And that's it! <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code>
+ And that's it! <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code>
is the lazy person's way to make a lazy funtion.
</p>
</div>
and a brief description of each.
</p>
<div class="table">
-<a name="boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.extends.t0"></a><p class="title"><b>Table 31.2. Expression Extension Macros</b></p>
+<a name="boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.extends.t0"></a><p class="title"><b>Table 32.2. Expression Extension Macros</b></p>
<div class="table-contents"><table class="table" summary="Expression Extension Macros">
<colgroup>
<col>
<code class="literal">as_child</code> vs. <code class="literal">as_expr</code></a>
</h6>
<p>
- Proto lets you independently customize the behavior of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code> and <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code>.
+ Proto lets you independently customize the behavior of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code> and <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code>.
Both accept an object <code class="literal">x</code> and return a Proto expression
by turning <code class="literal">x</code> it into a Proto terminal if necessary.
Although similar, the two functions are used in different situations
you want.
</p>
<p>
- To wit: <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code> is typically used by
+ To wit: <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code> is typically used by
<span class="emphasis"><em>you</em></span> to turn an object into a Proto expression that
is to be held in a local variable, as so:
</p>
is already a Proto expression or not. The object <code class="computeroutput"><span class="identifier">l</span></code>
is guaranteed to be a valid Proto expression. If <code class="computeroutput"><span class="identifier">x</span></code>
is a non-Proto object, it is turned into a terminal expression that holds
- <code class="computeroutput"><span class="identifier">x</span></code> <span class="emphasis"><em>by value</em></span>.<a href="#ftn.boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0" class="footnote" name="boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0"><sup class="footnote">[7]</sup></a> If <code class="computeroutput"><span class="identifier">x</span></code> is a
- Proto object already, <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code>
+ <code class="computeroutput"><span class="identifier">x</span></code> <span class="emphasis"><em>by value</em></span>.<a href="#ftn.boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0" class="footnote" name="boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0"><sup class="footnote">[12]</sup></a> If <code class="computeroutput"><span class="identifier">x</span></code> is a
+ Proto object already, <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code>
returns it <span class="emphasis"><em>by value</em></span> unmodified.
</p>
<p>
- In contrast, <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>
+ In contrast, <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>
is used internally by Proto to pre-process objects before making them
children of another expression. Since it's internal to Proto, you don't
see it explicitly, but it's there behind the scenes in expressions like
</pre>
<p>
In this case, Proto builds a plus node from the two children. Both are
- pre-processed by passing them to <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>
+ pre-processed by passing them to <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>
before making them children of the new node. If <code class="computeroutput"><span class="identifier">x</span></code>
is not a Proto expression, it becomes one by being wrapped in a Proto
- terminal that holds it <span class="emphasis"><em>by reference</em></span>. If <code class="computeroutput"><span class="identifier">x</span></code> is already a Proto expression, <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code> returns it <span class="emphasis"><em>by
+ terminal that holds it <span class="emphasis"><em>by reference</em></span>. If <code class="computeroutput"><span class="identifier">x</span></code> is already a Proto expression, <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code> returns it <span class="emphasis"><em>by
reference</em></span> unmodified. Contrast this with the above description
- for <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code>.
+ for <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code>.
</p>
<p>
The table below summarizes the above description.
</p>
<div class="table">
-<a name="boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.t0"></a><p class="title"><b>Table 31.3. proto::as_expr() vs. proto::as_child()</b></p>
+<a name="boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.t0"></a><p class="title"><b>Table 32.3. proto::as_expr() vs. proto::as_child()</b></p>
<div class="table-contents"><table class="table" summary="proto::as_expr() vs. proto::as_child()">
<colgroup>
<col>
<tr><td align="left" valign="top"><p>
There is one important place where Proto uses both <code class="computeroutput"><span class="identifier">as_expr</span></code>
<span class="emphasis"><em>and</em></span> <code class="computeroutput"><span class="identifier">as_child</span></code>:
- <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code>. The <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code> function requires
+ <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code>. The <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code> function requires
you to specify for each child whether it should be held by value or
- by reference. Proto uses <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code>
- to pre-process the children to be held by value, and <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code> for the ones to be
+ by reference. Proto uses <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code>
+ to pre-process the children to be held by value, and <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code> for the ones to be
held by reference.
</p></td></tr>
</table></div>
<p>
- Now that you know what <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>
- and <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code> are, where they are
+ Now that you know what <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>
+ and <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code> are, where they are
used, and what they do by default, you may decide that one or both of
these functions should have different behavior for your domain. For instance,
- given the above description of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>,
+ given the above description of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>,
the following code is always wrong:
</p>
<pre class="programlisting"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">literal</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">i</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>
<span class="keyword">auto</span> <span class="identifier">l</span> <span class="special">=</span> <span class="identifier">i</span> <span class="special">+</span> <span class="number">42</span><span class="special">;</span> <span class="comment">// This is WRONG! Don't do this.</span>
</pre>
<p>
- Why is this wrong? Because <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>
+ Why is this wrong? Because <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>
will turn the integer literal 42 into a Proto terminal that holds a reference
to a temporary integer initialized with 42. The lifetime of that temporary
ends at the semicolon, guaranteeing that the local <code class="computeroutput"><span class="identifier">l</span></code>
is left holding a dangling reference to a deceased integer. What to do?
- One answer is to use <code class="computeroutput"><a class="link" href="../boost/proto/deep_copy_idp926559920.html" title="Function template deep_copy">proto::deep_copy()</a></code>.
- Another is to customize the behavior of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>
+ One answer is to use <code class="computeroutput"><a class="link" href="../boost/proto/deep_copy_idp700354384.html" title="Function template deep_copy">proto::deep_copy()</a></code>.
+ Another is to customize the behavior of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>
for your domain. Read on for the details.
</p>
<h6>
</h6>
<p>
Although less common, Proto also lets you customize the behavior of
- <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code> on a per-domain basis.
+ <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code> on a per-domain basis.
The technique is identical to that for <code class="literal">as_child</code>. See
below:
</p>
<p>
Let's look again at the problem described above involving the C++11
<code class="computeroutput"><span class="keyword">auto</span></code> keyword and the default
- behavior of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code>.
+ behavior of <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code>.
</p>
<pre class="programlisting"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">literal</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">i</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>
<span class="keyword">auto</span> <span class="identifier">l</span> <span class="special">=</span> <span class="identifier">i</span> <span class="special">+</span> <span class="number">42</span><span class="special">;</span> <span class="comment">// This is WRONG! Don't do this.</span>
to hold the value 42. The local <code class="computeroutput"><span class="identifier">l</span></code>
will be left holding a dangling reference to it after its lifetime is
over. What if we want Proto to make expressions safe to store this way
- in local variables? We can do so very easily by making <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp930652976.html" title="Function as_child">proto::as_child()</a></code> behave just like <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code>. The following code
+ in local variables? We can do so very easily by making <code class="computeroutput"><a class="link" href="../boost/proto/as_child_idp704447440.html" title="Function as_child">proto::as_child()</a></code> behave just like <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code>. The following code
achieves this:
</p>
<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span> <span class="keyword">typename</span> <span class="identifier">E</span> <span class="special">></span>
</h6>
<p>
There is no simpler expression than a terminal, and no more basic operation
- than extracting its value. As we've already seen, that is what <code class="computeroutput"><a class="link" href="../boost/proto/value_idp930722544.html" title="Function value">proto::value()</a></code> is for.
+ than extracting its value. As we've already seen, that is what <code class="computeroutput"><a class="link" href="../boost/proto/value_idp704517008.html" title="Function value">proto::value()</a></code> is for.
</p>
<pre class="programlisting"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">terminal</span><span class="special"><</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span> <span class="special">&</span> <span class="special">>::</span><span class="identifier">type</span> <span class="identifier">cout_</span> <span class="special">=</span> <span class="special">{</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">};</span>
<span class="identifier">assert</span><span class="special">(</span> <span class="special">&</span><span class="identifier">sout</span> <span class="special">==</span> <span class="special">&</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">);</span>
</pre>
<p>
- To compute the return type of the <code class="computeroutput"><a class="link" href="../boost/proto/value_idp930722544.html" title="Function value">proto::value()</a></code>
+ To compute the return type of the <code class="computeroutput"><a class="link" href="../boost/proto/value_idp704517008.html" title="Function value">proto::value()</a></code>
function, you can use <code class="computeroutput"><a class="link" href="../boost/proto/result_of/value.html" title="Struct template value">proto::result_of::value<></a></code>.
When the parameter to <code class="computeroutput"><a class="link" href="../boost/proto/result_of/value.html" title="Struct template value">proto::result_of::value<></a></code>
is a non-reference type, the result type of the metafunction is the type
The following table summarizes the above paragraph.
</p>
<div class="table">
-<a name="boost_proto.users_guide.intermediate_form.left_right_child.t0"></a><p class="title"><b>Table 31.4. Accessing Value Types</b></p>
+<a name="boost_proto.users_guide.intermediate_form.left_right_child.t0"></a><p class="title"><b>Table 32.4. Accessing Value Types</b></p>
<div class="table-contents"><table class="table" summary="Accessing Value Types">
<colgroup>
<col>
<p>
Each non-terminal node in an expression tree corresponds to an operator
in an expression, and the children correspond to the operands, or arguments
- of the operator. To access them, you can use the <code class="computeroutput"><a class="link" href="../boost/proto/child_c_idp930703488.html" title="Function child_c">proto::child_c()</a></code>
+ of the operator. To access them, you can use the <code class="computeroutput"><a class="link" href="../boost/proto/child_c_idp704497952.html" title="Function child_c">proto::child_c()</a></code>
function template, as demonstrated below:
</p>
<pre class="programlisting"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">terminal</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">i</span> <span class="special">=</span> <span class="special">{</span><span class="number">42</span><span class="special">};</span>
metafunction.
</p>
<div class="table">
-<a name="boost_proto.users_guide.intermediate_form.left_right_child.t1"></a><p class="title"><b>Table 31.5. Accessing Child Types</b></p>
+<a name="boost_proto.users_guide.intermediate_form.left_right_child.t1"></a><p class="title"><b>Table 32.5. Accessing Child Types</b></p>
<div class="table-contents"><table class="table" summary="Accessing Child Types">
<colgroup>
<col>
<p>
Most operators in C++ are unary or binary, so accessing the only operand,
or the left and right operands, are very common operations. For this reason,
- Proto provides the <code class="computeroutput"><a class="link" href="../boost/proto/child_idp930674992.html" title="Function child">proto::child()</a></code>,
- <code class="computeroutput"><a class="link" href="../boost/proto/left_idp930735008.html" title="Function left">proto::left()</a></code>, and <code class="computeroutput"><a class="link" href="../boost/proto/right_idp930749488.html" title="Function right">proto::right()</a></code>
- functions. <code class="computeroutput"><a class="link" href="../boost/proto/child_idp930674992.html" title="Function child">proto::child()</a></code> and <code class="computeroutput"><a class="link" href="../boost/proto/left_idp930735008.html" title="Function left">proto::left()</a></code>
+ Proto provides the <code class="computeroutput"><a class="link" href="../boost/proto/child_idp704469456.html" title="Function child">proto::child()</a></code>,
+ <code class="computeroutput"><a class="link" href="../boost/proto/left_idp704529472.html" title="Function left">proto::left()</a></code>, and <code class="computeroutput"><a class="link" href="../boost/proto/right_idp704543952.html" title="Function right">proto::right()</a></code>
+ functions. <code class="computeroutput"><a class="link" href="../boost/proto/child_idp704469456.html" title="Function child">proto::child()</a></code> and <code class="computeroutput"><a class="link" href="../boost/proto/left_idp704529472.html" title="Function left">proto::left()</a></code>
are synonymous with <code class="computeroutput"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">child_c</span><span class="special"><</span><span class="number">0</span><span class="special">>()</span></code>,
- and <code class="computeroutput"><a class="link" href="../boost/proto/right_idp930749488.html" title="Function right">proto::right()</a></code> is synonymous with <code class="computeroutput"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">child_c</span><span class="special"><</span><span class="number">1</span><span class="special">>()</span></code>.
+ and <code class="computeroutput"><a class="link" href="../boost/proto/right_idp704543952.html" title="Function right">proto::right()</a></code> is synonymous with <code class="computeroutput"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">child_c</span><span class="special"><</span><span class="number">1</span><span class="special">>()</span></code>.
</p>
<p>
There are also <code class="computeroutput"><a class="link" href="../boost/proto/result_of/child.html" title="Struct template child">proto::result_of::child<></a></code>,
all intermediate nodes and the terminals are held <span class="emphasis"><em>by value</em></span>.
That way, you can safely assign the expression template to a local variable
or return it from a function without worrying about dangling references.
- You can do this with <code class="computeroutput"><a class="link" href="../boost/proto/deep_copy_idp926559920.html" title="Function template deep_copy">proto::deep_copy()</a></code>
+ You can do this with <code class="computeroutput"><a class="link" href="../boost/proto/deep_copy_idp700354384.html" title="Function template deep_copy">proto::deep_copy()</a></code>
as fo llows:
</p>
<pre class="programlisting"><span class="comment">// OK, "ex" has no dangling references</span>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
- <code class="computeroutput"><a class="link" href="../boost/proto/deep_copy_idp926559920.html" title="Function template deep_copy">proto::deep_copy()</a></code> makes no exception for
+ <code class="computeroutput"><a class="link" href="../boost/proto/deep_copy_idp700354384.html" title="Function template deep_copy">proto::deep_copy()</a></code> makes no exception for
arrays, which it stores by value. That can potentially cause a large
amount of data to be copied.
</p></td></tr>
</h4></div></div></div>
<p>
Proto provides a utility for pretty-printing expression trees that comes
- in very handy when you're trying to debug your EDSL. It's called <code class="computeroutput"><a class="link" href="../boost/proto/display_expr_idp926457440.html" title="Function display_expr">proto::display_expr()</a></code>, and you pass it the expression
+ in very handy when you're trying to debug your EDSL. It's called <code class="computeroutput"><a class="link" href="../boost/proto/display_expr_idp700251904.html" title="Function display_expr">proto::display_expr()</a></code>, and you pass it the expression
to print and optionally, an <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span></code>
to which to send the output. Consider:
</p>
, terminal(42)
)</pre>
<p>
- In order to call <code class="computeroutput"><a class="link" href="../boost/proto/display_expr_idp926457440.html" title="Function display_expr">proto::display_expr()</a></code>,
+ In order to call <code class="computeroutput"><a class="link" href="../boost/proto/display_expr_idp700251904.html" title="Function display_expr">proto::display_expr()</a></code>,
all the terminals in the expression must be Streamable (that is, they can
be written to a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span></code>). In addition, the tag types
must all be Streamable as well. Here is an example that includes a custom
usable as grammars for matching such nodes, as well as pass-through transforms.
</p>
<div class="table">
-<a name="boost_proto.users_guide.intermediate_form.tags_and_metafunctions.t0"></a><p class="title"><b>Table 31.6. Operators, Tags and Metafunctions</b></p>
+<a name="boost_proto.users_guide.intermediate_form.tags_and_metafunctions.t0"></a><p class="title"><b>Table 32.6. Operators, Tags and Metafunctions</b></p>
<div class="table-contents"><table class="table" summary="Operators, Tags and Metafunctions">
<colgroup>
<col>
<span class="special">+</span> <span class="number">2</span> <span class="special">+</span> <span class="number">3</span> <span class="special">+</span>
<span class="number">4</span></code> does not describe a flat sequence
of terminals --- it describes a binary tree. We can treat it as a flat
- sequence of terminals, however, using Proto's <code class="computeroutput"><a class="link" href="../boost/proto/flatten_idp927608160.html" title="Function flatten">proto::flatten()</a></code>
- function. <code class="computeroutput"><a class="link" href="../boost/proto/flatten_idp927608160.html" title="Function flatten">proto::flatten()</a></code> returns a view which makes
+ sequence of terminals, however, using Proto's <code class="computeroutput"><a class="link" href="../boost/proto/flatten_idp701402624.html" title="Function flatten">proto::flatten()</a></code>
+ function. <code class="computeroutput"><a class="link" href="../boost/proto/flatten_idp701402624.html" title="Function flatten">proto::flatten()</a></code> returns a view which makes
a tree appear as a flat Fusion sequence. If the top-most node has a tag
type <code class="computeroutput"><span class="identifier">T</span></code>, then the elements
of the flattened sequence are the child nodes that do <span class="emphasis"><em>not</em></span>
and references.
</p>
<div class="table">
-<a name="boost_proto.users_guide.intermediate_form.expression_introspection.fuzzy_and_exact_matches_of_terminals.t0"></a><p class="title"><b>Table 31.7. proto::matches<> and Reference / CV-Qualification of Terminals</b></p>
+<a name="boost_proto.users_guide.intermediate_form.expression_introspection.fuzzy_and_exact_matches_of_terminals.t0"></a><p class="title"><b>Table 32.7. proto::matches<> and Reference / CV-Qualification of Terminals</b></p>
<div class="table-contents"><table class="table" summary="proto::matches<> and Reference / CV-Qualification of Terminals">
<colgroup>
<col>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
A <span class="emphasis"><em>context</em></span> is like a function object that you pass
- along with an expression to the <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>
- function. It associates behaviors with node types. <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>
+ along with an expression to the <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>
+ function. It associates behaviors with node types. <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>
walks the expression and invokes your context at each node.
</li>
<li class="listitem">
</dl></div>
<p>
Once you have constructed a Proto expression tree, either by using Proto's
- operator overloads or with <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp927901888.html" title="Function make_expr">proto::make_expr()</a></code>
+ operator overloads or with <code class="computeroutput"><a class="link" href="../boost/proto/make_expr_idp701696352.html" title="Function make_expr">proto::make_expr()</a></code>
and friends, you probably want to actually <span class="emphasis"><em>do</em></span> something
with it. The simplest option is to use <code class="computeroutput"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">eval</span><span class="special">()</span></code>, a generic expression evaluator. To use
- <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>, you'll need to define
- a <span class="emphasis"><em>context</em></span> that tells <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>
+ <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>, you'll need to define
+ a <span class="emphasis"><em>context</em></span> that tells <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>
how each node should be evaluated. This section goes through the nuts and
- bolts of using <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>, defining evaluation contexts,
+ bolts of using <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>, defining evaluation contexts,
and using the contexts that Proto provides.
</p>
<div class="note"><table border="0" summary="Note">
<span class="special">}</span>
</pre>
<p>
- Given an expression and an evaluation context, using <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>
- is quite simple. Simply pass the expression and the context to <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code> and it does the rest
+ Given an expression and an evaluation context, using <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>
+ is quite simple. Simply pass the expression and the context to <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code> and it does the rest
and returns the result. You can use the <code class="computeroutput"><span class="identifier">eval</span><span class="special"><></span></code> metafunction in the <code class="computeroutput"><span class="identifier">proto</span><span class="special">::</span><span class="identifier">result_of</span></code> namespace to compute the
- return type of <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>. The following demonstrates
- a use of <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>:
+ return type of <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>. The following demonstrates
+ a use of <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>:
</p>
<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Expr</span><span class="special">></span>
<span class="keyword">typename</span> <span class="identifier">proto</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">eval</span><span class="special"><</span><span class="identifier">Expr</span> <span class="keyword">const</span><span class="special">,</span> <span class="identifier">MyContext</span><span class="special">>::</span><span class="identifier">type</span>
<span class="special">}</span>
</pre>
<p>
- What <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code> does is also very simple.
+ What <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code> does is also very simple.
It defers most of the work to the context itself. Here essentially is
- the implementation of <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code>:
+ the implementation of <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code>:
</p>
<pre class="programlisting"><span class="comment">// eval() dispatches to a nested "eval<>" function</span>
<span class="comment">// object within the Context:</span>
<span class="special">}</span>
</pre>
<p>
- Really, <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code> is nothing more than
+ Really, <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code> is nothing more than
a thin wrapper that dispatches to the appropriate handler within the
context class. In the next section, we'll see how to implement a context
class from scratch.
an Evaluation Context</a>
</h5></div></div></div>
<p>
- As we saw in the previous section, there is really not much to the <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code> function. Rather, all
+ As we saw in the previous section, there is really not much to the <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code> function. Rather, all
the interesting expression evaluation goes on within a context class.
This section shows how to implement one from scratch.
</p>
<span class="special">};</span>
</pre>
<p>
- Now we can use <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp926727360.html" title="Function eval">proto::eval()</a></code> with the context class
+ Now we can use <code class="computeroutput"><a class="link" href="../boost/proto/eval_idp700521824.html" title="Function eval">proto::eval()</a></code> with the context class
above to evaluate calculator expressions as follows:
</p>
<pre class="programlisting"><span class="comment">// Evaluate an expression with a calculator_context</span>
these 5 constituents.
</p>
<div class="table">
-<a name="boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.t0"></a><p class="title"><b>Table 31.8. Calculator Sub-Expression Arities</b></p>
+<a name="boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.t0"></a><p class="title"><b>Table 32.8. Calculator Sub-Expression Arities</b></p>
<div class="table-contents"><table class="table" summary="Calculator Sub-Expression Arities">
<colgroup>
<col>
</pre>
<p>
... they will get a compile-time error message on the line with the assertion
- that reads something like this<a href="#ftn.boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0" class="footnote" name="boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0"><sup class="footnote">[8]</sup></a>:
+ that reads something like this<a href="#ftn.boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0" class="footnote" name="boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0"><sup class="footnote">[13]</sup></a>:
</p>
<pre class="programlisting">c:\boost\org\trunk\libs\proto\scratch\main.cpp(97) : error C2664: 'boost::mpl::asse
rtion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************boo
The same is true for any primitive transform. The following are all equivalent:
</p>
<div class="table">
-<a name="boost_proto.users_guide.back_end.expression_transformation.implicit_params.t0"></a><p class="title"><b>Table 31.9. Implicit Parameters to Primitive Transforms</b></p>
+<a name="boost_proto.users_guide.back_end.expression_transformation.implicit_params.t0"></a><p class="title"><b>Table 32.9. Implicit Parameters to Primitive Transforms</b></p>
<div class="table-contents"><table class="table" summary="Implicit Parameters to Primitive Transforms">
<colgroup><col></colgroup>
<thead><tr><th>
these two transforms are equivalent. Can you see why?
</p>
<div class="table">
-<a name="boost_proto.users_guide.back_end.expression_transformation.implicit_params.t1"></a><p class="title"><b>Table 31.10. Two Equivalent Transforms</b></p>
+<a name="boost_proto.users_guide.back_end.expression_transformation.implicit_params.t1"></a><p class="title"><b>Table 32.10. Two Equivalent Transforms</b></p>
<div class="table-contents"><table class="table" summary="Two Equivalent Transforms">
<colgroup>
<col>
transforms:
</p>
<div class="table">
-<a name="boost_proto.users_guide.back_end.expression_transformation.canned_transforms.t0"></a><p class="title"><b>Table 31.11. Class Templates With Pass-Through Transforms</b></p>
+<a name="boost_proto.users_guide.back_end.expression_transformation.canned_transforms.t0"></a><p class="title"><b>Table 32.11. Class Templates With Pass-Through Transforms</b></p>
<div class="table-contents"><table class="table" summary="Class Templates With Pass-Through Transforms">
<colgroup><col></colgroup>
<thead><tr><th>
generally useful. They are specified in the table below:
</p>
<div class="table">
-<a name="boost_proto.users_guide.back_end.expression_transformation.primitives.t0"></a><p class="title"><b>Table 31.12. proto::transform_impl<Expr, State, Data> typedefs</b></p>
+<a name="boost_proto.users_guide.back_end.expression_transformation.primitives.t0"></a><p class="title"><b>Table 32.12. proto::transform_impl<Expr, State, Data> typedefs</b></p>
<div class="table-contents"><table class="table" summary="proto::transform_impl<Expr, State, Data> typedefs">
<colgroup>
<col>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
-<div id="ftn.boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0" class="footnote"><p><a href="#boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0" class="para"><sup class="para">[7] </sup></a>
- It's not always possible to hold something by value. By default, <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp930631008.html" title="Function as_expr">proto::as_expr()</a></code> makes an exception
+<div id="ftn.boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0" class="footnote"><p><a href="#boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child.f0" class="para"><sup class="para">[12] </sup></a>
+ It's not always possible to hold something by value. By default, <code class="computeroutput"><a class="link" href="../boost/proto/as_expr_idp704425472.html" title="Function as_expr">proto::as_expr()</a></code> makes an exception
for functions, abstract types, and iostreams (types derived from <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span></code>). These objects are held
by reference. All others are held by value, even arrays.
</p></div>
-<div id="ftn.boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0" class="footnote"><p><a href="#boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0" class="para"><sup class="para">[8] </sup></a>
+<div id="ftn.boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0" class="footnote"><p><a href="#boost_proto.users_guide.back_end.expression_transformation.example__calculator_arity.f0" class="para"><sup class="para">[13] </sup></a>
This error message was generated with Microsoft Visual C++ 9.0. Different
compilers will emit different messages with varying degrees of readability.
</p></div>