1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Struct pack</title> 6<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../../proto/reference.html#header.boost.proto.transform.impl_hpp" title="Header <boost/proto/transform/impl.hpp>"> 10<link rel="prev" href="transform_impl.html" title="Struct template transform_impl"> 11<link rel="next" href="integral_c.html" title="Struct template integral_c"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> 16<td align="center"><a href="../../../../index.html">Home</a></td> 17<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="transform_impl.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../proto/reference.html#header.boost.proto.transform.impl_hpp"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="integral_c.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="refentry"> 27<a name="boost.proto.pack"></a><div class="titlepage"></div> 28<div class="refnamediv"> 29<h2><span class="refentrytitle">Struct pack</span></h2> 30<p>boost::proto::pack — To turn an expression into a pseudo-parameter pack containing the 31 expression's children, for the purpose of expanding the pack expression within 32 a <a class="link" href="../../CallableTransform.html" title="Concept CallableTransform">CallableTransform</a> or 33 <a class="link" href="../../ObjectTransform.html" title="Concept ObjectTransform">ObjectTransform</a>.</p> 34</div> 35<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2> 36<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: <<a class="link" href="../../proto/reference.html#header.boost.proto.transform.impl_hpp" title="Header <boost/proto/transform/impl.hpp>">boost/proto/transform/impl.hpp</a>> 37 38</span> 39<span class="keyword">struct</span> <a class="link" href="pack.html" title="Struct pack">pack</a> <span class="special">{</span> 40<span class="special">}</span><span class="special">;</span></pre></div> 41<div class="refsect1"> 42<a name="id-1.3.33.5.49.6.4"></a><h2>Description</h2> 43<p> 44 <code class="computeroutput">proto::pack</code> is useful within 45 <a class="link" href="../../CallableTransform.html" title="Concept CallableTransform">CallableTransform</a>s and 46 <a class="link" href="../../ObjectTransform.html" title="Concept ObjectTransform">ObjectTransform</a>s when one wishes to unpack an expression 47 into a function call or an object constructor. <code class="computeroutput">proto::pack</code> 48 turns a Proto expression into a pseudo-parameter pack, which may appear in an unpacking 49 pattern to be expanded with the "<code class="computeroutput">...</code>" syntax. 50 </p> 51<p> 52 <span class="bold"><strong>Example:</strong></span> 53 </p> 54<p> 55 </p> 56<pre class="programlisting"><span class="comment">// The following demonstrates how to use a pseudo-pack expansion</span> 57<span class="comment">// to unpack an expression into a function call.</span> 58 59<span class="keyword">struct</span> <span class="identifier">do_sum</span> <span class="special">:</span> <a class="link" href="callable.html" title="Struct callable">proto::callable</a> 60<span class="special">{</span> 61 <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">result_type</span><span class="special">;</span> 62 63 <span class="keyword">int</span> <span class="keyword">operator</span><span class="special">(</span><span class="special">)</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">}</span> 64 <span class="keyword">int</span> <span class="keyword">operator</span><span class="special">(</span><span class="special">)</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">j</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">i</span> <span class="special">+</span> <span class="identifier">j</span><span class="special">;</span> <span class="special">}</span> 65 <span class="keyword">int</span> <span class="keyword">operator</span><span class="special">(</span><span class="special">)</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">j</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">k</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">i</span> <span class="special">+</span> <span class="identifier">j</span> <span class="special">+</span> <span class="identifier">k</span><span class="special">;</span> <span class="special">}</span> 66<span class="special">}</span><span class="special">;</span> 67 68<span class="comment">// Take any n-ary expression where the children are all int terminals and sum all the ints</span> 69<span class="keyword">struct</span> <span class="identifier">sum</span> 70 <span class="special">:</span> <a class="link" href="when.html" title="Struct template when">proto::when</a><span class="special"><</span> 71 72 <span class="comment">// Match any nary expression where the children are all int terminals</span> 73 <a class="link" href="nary_expr.html" title="Struct template nary_expr">proto::nary_expr</a><span class="special"><</span><a class="link" href="_.html" title="Struct _">_</a><span class="special">,</span> <a class="link" href="vararg.html" title="Struct template vararg">proto::vararg</a><span class="special"><</span><a class="link" href="terminal.html" title="Struct template terminal">proto::terminal</a><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">></span> <span class="special">></span> 74 75 <span class="comment">// Turn the current expression into a pseudo-parameter pack, then expand it,</span> 76 <span class="comment">// extracting the value from each child in turn.</span> 77 <span class="special">,</span> <span class="identifier">do_sum</span><span class="special">(</span><a class="link" href="_value.html" title="Struct _value">proto::_value</a><span class="special">(</span><span class="identifier">proto</span><span class="special">::</span><span class="identifier">pack</span><span class="special">(</span><a class="link" href="_.html" title="Struct _">_</a><span class="special">)</span><span class="special">)</span><span class="special">...</span><span class="special">)</span> 78 <span class="special">></span> 79<span class="special">{</span><span class="special">}</span><span class="special">;</span> 80 81<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="special">)</span> 82<span class="special">{</span> 83 <a class="link" href="terminal.html" title="Struct template terminal">proto::terminal</a><span class="special"><</span><span class="keyword">int</span><span class="special">></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><span class="special">;</span> 84 <span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">sum</span><span class="special">(</span><span class="special">)</span><span class="special">(</span> <span class="identifier">i</span><span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="number">5</span><span class="special">)</span> <span class="special">)</span><span class="special">;</span> <span class="comment">// Creates a ternary functional-call expression</span> 85 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Sum of 42, 3, and 5 : "</span> <span class="special"><<</span> <span class="identifier">result</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 86<span class="special">}</span></pre> 87<p> 88 </p> 89<p> 90 The above program displays: 91 </p> 92<p> 93 <code class="computeroutput">Sum of 42, 3, and 5 : 50</code> 94 </p> 95<p> 96 In the above example, the type 97 <code class="computeroutput"> 98 <a class="link" href="_value.html" title="Struct _value">proto::_value</a>(proto::pack(<a class="link" href="_.html" title="Struct _">_</a>)) 99 </code> 100 is a so-called <span class="emphasis"><em>unpacking pattern</em></span>, described below. 101 </p> 102<p> 103 <span class="bold"><strong>Unpacking Patterns:</strong></span> 104 </p> 105<p> 106 Composite transforms (either <a class="link" href="../../CallableTransform.html" title="Concept CallableTransform">CallableTransform</a>s or 107 <a class="link" href="../../ObjectTransform.html" title="Concept ObjectTransform">ObjectTransform</a>s) usually have the form 108 <code class="computeroutput">X(A<sub>0</sub>,…A<sub>n</sub>)</code>. 109 However, when the argument list in a composite transform is terminated with a C-style 110 vararg ellipsis as in <code class="computeroutput">X(A<sub>0</sub>,…A<sub>n</sub> ...)</code>, 111 the final argument <code class="computeroutput">A<sub>n</sub></code> is treated 112 as an <span class="emphasis"><em>unpacking pattern</em></span>. 113 </p> 114<p> 115 An unpacking pattern must itself be a composite transform; that is, it must be a 116 function type representing either a <a class="link" href="../../CallableTransform.html" title="Concept CallableTransform">CallableTransform</a> or 117 an <a class="link" href="../../ObjectTransform.html" title="Concept ObjectTransform">ObjectTransform</a>. The type <code class="computeroutput">proto::pack(_)</code> 118 must appear exactly once in the unpacking pattern. This type will receive a substitution 119 when the unpacking pattern is expanded. 120 </p> 121<p> 122 A composite transform like <code class="computeroutput">X(A<sub>0</sub>,…A<sub>n</sub> ...)</code>, 123 when evaluated against a given expression <em class="replaceable"><code>E</code></em>, state and data, is evaluated as if it were 124 <code class="computeroutput">X(A<sub>0</sub>,…A<sub>n-1</sub>,<em class="replaceable"><code>S</code></em>)</code> 125 where <em class="replaceable"><code>S</code></em> is a type sequence computed as follows: 126 </p> 127<p> 128 Let <code class="computeroutput"><em class="replaceable"><code>SUB</code></em>(A,B)</code> be a type function that replaces every occurence of 129 <code class="computeroutput">proto::pack(_)</code> within <code class="computeroutput">A</code> with <code class="computeroutput">B</code>. 130 </p> 131<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 132<li class="listitem"> 133 If the expression <em class="replaceable"><code>E</code></em> is a terminal (i.e. it has arity 0), <em class="replaceable"><code>S</code></em> 134 is the one-element sequence containing <code class="computeroutput"><em class="replaceable"><code>SUB</code></em>(A<sub>n</sub>, <a class="link" href="_value.html" title="Struct _value">proto::_value</a>)</code>. 135 </li> 136<li class="listitem"> 137 If the expression <em class="replaceable"><code>E</code></em> is a non-terminal, <em class="replaceable"><code>S</code></em> is the sequence 138 <code class="computeroutput"><em class="replaceable"><code>SUB</code></em>(A<sub>n</sub>, <a class="link" href="_child_c.html" title="Struct template _child_c">proto::_child_c</a><0>),… 139 <em class="replaceable"><code>SUB</code></em>(A<sub>n</sub>, <a class="link" href="_child_c.html" title="Struct template _child_c">proto::_child_c</a><<em class="replaceable"><code>M</code></em>-1>)</code>, where 140 <em class="replaceable"><code>M</code></em> is the arity of the expression <em class="replaceable"><code>E</code></em>. 141 </li> 142</ul></div> 143<p> 144 </p> 145</div> 146</div> 147<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 148<td align="left"></td> 149<td align="right"><div class="copyright-footer">Copyright © 2008 Eric Niebler<p> 150 Distributed under the Boost Software License, Version 1.0. (See accompanying 151 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 152 </p> 153</div></td> 154</tr></table> 155<hr> 156<div class="spirit-nav"> 157<a accesskey="p" href="transform_impl.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../proto/reference.html#header.boost.proto.transform.impl_hpp"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="integral_c.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 158</div> 159</body> 160</html> 161