1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Values</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Phoenix 3.2.0"> 8<link rel="up" href="../starter_kit.html" title="Starter Kit"> 9<link rel="prev" href="../starter_kit.html" title="Starter Kit"> 10<link rel="next" href="references.html" title="References"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="../starter_kit.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../starter_kit.html"><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="references.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="phoenix.starter_kit.values"></a><a class="link" href="values.html" title="Values">Values</a> 28</h3></div></div></div> 29<p> 30 Values are functions! Examples: 31 </p> 32<pre class="programlisting"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> 33<span class="identifier">val</span><span class="special">(</span><span class="string">"Hello, World"</span><span class="special">)</span> 34</pre> 35<p> 36 The first evaluates to a nullary function (a function taking no arguments) 37 that returns an <code class="computeroutput"><span class="keyword">int</span></code>, <code class="computeroutput"><span class="number">3</span></code>. The second evaluates to a nullary function 38 that returns a <code class="computeroutput"><span class="keyword">char</span> <span class="keyword">const</span><span class="special">(&)[</span><span class="number">13</span><span class="special">]</span></code>, <code class="computeroutput"><span class="string">"Hello, 39 World"</span></code>. 40 </p> 41<h5> 42<a name="phoenix.starter_kit.values.h0"></a> 43 <span class="phrase"><a name="phoenix.starter_kit.values.lazy_evaluation"></a></span><a class="link" href="values.html#phoenix.starter_kit.values.lazy_evaluation">Lazy 44 Evaluation</a> 45 </h5> 46<p> 47 Confused? <code class="computeroutput"><span class="identifier">val</span></code> is a unary 48 function and <code class="computeroutput"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span></code> invokes 49 it, you say? Yes. However, read carefully: <span class="emphasis"><em>"evaluates to a 50 nullary function"</em></span>. <code class="computeroutput"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span></code> 51 evaluates to (returns) a nullary function. Aha! <code class="computeroutput"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span></code> 52 returns a function! So, since <code class="computeroutput"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span></code> 53 returns a function, you can invoke it. Example: 54 </p> 55<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 56</pre> 57<p> 58 (See <a href="../../../../example/values.cpp" target="_top">values.cpp</a>) 59 </p> 60<div class="blurb"> 61<div class="titlepage"><div><div><p class="title"><b></b></p></div></div></div> 62<p> 63 <span class="inlinemediaobject"><img src="../../images/tip.png"></span> 64 Learn more about values <a class="link" href="../modules/core/values.html" title="Values">here.</a> 65 </p> 66</div> 67<p> 68 The second function call (the one with no arguments) calls the nullary function 69 which then returns <code class="computeroutput"><span class="number">3</span></code>. The need 70 for a second function call is the reason why the function is said to be 71 <span class="bold"><strong><span class="emphasis"><em>Lazily Evaluated</em></span></strong></span>. The 72 first call doesn't do anything. You need a second call to finally evaluate 73 the thing. The first call lazily evaluates the function; i.e. doesn't do 74 anything and defers the evaluation for later. 75 </p> 76<h5> 77<a name="phoenix.starter_kit.values.h1"></a> 78 <span class="phrase"><a name="phoenix.starter_kit.values.callbacks"></a></span><a class="link" href="values.html#phoenix.starter_kit.values.callbacks">Callbacks</a> 79 </h5> 80<p> 81 It may not be immediately apparent how lazy evaluation can be useful by just 82 looking at the example above. Putting the first and second function call 83 in a single line is really not very useful. However, thinking of <code class="computeroutput"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span></code> as a callback function (and in most cases 84 they are actually used that way), will make it clear. Example: 85 </p> 86<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">F</span><span class="special">></span> 87<span class="keyword">void</span> <span class="identifier">print</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">)</span> 88<span class="special">{</span> 89 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">f</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 90<span class="special">}</span> 91 92<span class="keyword">int</span> 93<span class="identifier">main</span><span class="special">()</span> 94<span class="special">{</span> 95 <span class="identifier">print</span><span class="special">(</span><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">));</span> 96 <span class="identifier">print</span><span class="special">(</span><span class="identifier">val</span><span class="special">(</span><span class="string">"Hello World"</span><span class="special">));</span> 97 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 98<span class="special">}</span> 99</pre> 100<p> 101 (See <a href="../../../../example/callback.cpp" target="_top">callback.cpp</a>) 102 </p> 103</div> 104<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 105<td align="left"></td> 106<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2010, 2014, 2015 Joel de Guzman, Dan Marsden, Thomas 107 Heller, John Fletcher<p> 108 Distributed under the Boost Software License, Version 1.0. (See accompanying 109 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>) 110 </p> 111</div></td> 112</tr></table> 113<hr> 114<div class="spirit-nav"> 115<a accesskey="p" href="../starter_kit.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../starter_kit.html"><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="references.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 116</div> 117</body> 118</html> 119