/Src/Dependencies/Boost/libs/phoenix/doc/html/phoenix/starter_kit/values.html

http://hadesmem.googlecode.com/ · HTML · 114 lines · 113 code · 1 blank · 0 comment · 0 complexity · 85fb30f966b68d29a5e853edf50f1ff7 MD5 · raw file

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  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.76.1">
  7. <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Phoenix 3.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">(&amp;)[</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. <a name="phoenix.starter_kit.values.lazy_evaluation"></a><h5>
  42. <a name="id801318"></a>
  43. <a class="link" href="values.html#phoenix.starter_kit.values.lazy_evaluation">Lazy Evaluation</a>
  44. </h5>
  45. <p>
  46. Confused? <code class="computeroutput"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span></code> is a unary
  47. function, you say? Yes it is. However, read carefully: <span class="emphasis"><em>"evaluates
  48. to a 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>
  49. 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>
  50. 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>
  51. returns a function, you can invoke it. Example:
  52. </p>
  53. <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
  54. </pre>
  55. <p>
  56. (See <a href="../../../../example/values.cpp" target="_top">values.cpp</a>)
  57. </p>
  58. <div class="sidebar">
  59. <div class="titlepage"></div>
  60. <p>
  61. <span class="inlinemediaobject"><img src="../../images/tip.png" alt="tip"></span> Learn more about values <a class="link" href="../modules/core/values.html" title="Values">here.</a>
  62. </p>
  63. </div>
  64. <p>
  65. The second function call (the one with no arguments) calls the nullary function
  66. which then returns <code class="computeroutput"><span class="number">3</span></code>. The need
  67. for a second function call is the reason why the function is said to be
  68. <span class="bold"><strong><span class="emphasis"><em>Lazily Evaluated</em></span></strong></span>. The
  69. first call doesn't do anything. You need a second call to finally evaluate
  70. the thing. The first call lazily evaluates the function; i.e. doesn't do
  71. anything and defers the evaluation for later.
  72. </p>
  73. <a name="phoenix.starter_kit.values.callbacks"></a><h5>
  74. <a name="id801543"></a>
  75. <a class="link" href="values.html#phoenix.starter_kit.values.callbacks">Callbacks</a>
  76. </h5>
  77. <p>
  78. It may not be immediately apparent how lazy evaluation can be useful by just
  79. looking at the example above. Putting the first and second function call
  80. 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
  81. they are actually used that way), will make it clear. Example:
  82. </p>
  83. <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">F</span><span class="special">&gt;</span>
  84. <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>
  85. <span class="special">{</span>
  86. <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">f</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
  87. <span class="special">}</span>
  88. <span class="keyword">int</span>
  89. <span class="identifier">main</span><span class="special">()</span>
  90. <span class="special">{</span>
  91. <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>
  92. <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>
  93. <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
  94. <span class="special">}</span>
  95. </pre>
  96. <p>
  97. (See <a href="../../../../example/callback.cpp" target="_top">callback.cpp</a>)
  98. </p>
  99. </div>
  100. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  101. <td align="left"></td>
  102. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p>
  103. Distributed under the Boost Software License, Version 1.0. (See accompanying
  104. 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>)
  105. </p>
  106. </div></td>
  107. </tr></table>
  108. <hr>
  109. <div class="spirit-nav">
  110. <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>
  111. </div>
  112. </body>
  113. </html>