metaprogrammg3.html 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3. <title>Alternatives - Boost.Outcome documentation</title>
  4. <link rel="stylesheet" href="../../../css/boost.css" type="text/css">
  5. <meta name="generator" content="Hugo 0.52 with Boostdoc theme">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
  7. <link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../../../tutorial/advanced/constructors/metaprogrammg2.html"><img src="../../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../../tutorial/advanced/constructors.html"><img src="../../../images/up.png" alt="Up"></a>
  11. <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Alternatives</h1></div></div></div>
  13. <p>No doubt many will dislike the two-stage invocation pattern i.e.</p>
  14. <div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">make</span><span class="o">&lt;</span><span class="n">file_handle</span><span class="o">&gt;</span><span class="p">{</span><span class="s">&#34;hello&#34;</span><span class="p">}();</span>
  15. </code></pre></div>
  16. <p>So let us examine the most obvious alternative: a templated free function <code>make&lt;T&gt;</code>.</p>
  17. <p>Due to the inability to partially specialise templated functions in C++, you
  18. need to use tagged overloading e.g.</p>
  19. <div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span><span class="err">... </span><span class="nc">Args</span><span class="o">&gt;</span>
  20. <span class="kr">inline</span> <span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">file_handle</span><span class="o">&gt;</span> <span class="n">make</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">in_place_type_t</span><span class="o">&lt;</span><span class="n">file_handle</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">Args</span><span class="o">&amp;&amp;</span> <span class="p">...</span> <span class="n">args</span><span class="p">)</span>
  21. <span class="p">{</span>
  22. <span class="k">return</span> <span class="n">file_handle</span><span class="o">::</span><span class="n">file</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">Args</span><span class="o">&gt;</span><span class="p">(</span><span class="n">args</span><span class="p">)...);</span>
  23. <span class="p">}</span>
  24. <span class="p">...</span>
  25. <span class="c1">// Now you must always write this:
  26. </span><span class="c1"></span><span class="n">make</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">in_place_type</span><span class="o">&lt;</span><span class="n">file_handle</span><span class="o">&gt;</span><span class="p">,</span> <span class="s">&#34;hello&#34;</span><span class="p">);</span>
  27. </code></pre></div>
  28. <p>Tagged overloading is fine for smaller projects, but for larger code bases:</p>
  29. <ol>
  30. <li>It takes longer to type <code>make(std::in_place_type&lt;file_handle&gt;, &quot;hello&quot;)</code>,
  31. and is possibly less intuitive to write,
  32. than it does <code>make&lt;file_handle&gt;{&quot;hello&quot;}()</code>.</li>
  33. <li>Compiler error messages are enormously clearer if you encode the permitted
  34. overloads for construction into the <code>make&lt;file_handle&gt;</code> type rather than
  35. letting a variadic free function fail to resolve an appropriate overload.</li>
  36. <li>Resolving variadic free function overloads is not constant time for the compiler,
  37. whereas resolving the type specialisation for <code>make&lt;file_handle&gt;</code>
  38. is constant time. In other words, free functions are <em>expensive</em> on build
  39. times, whereas fully specialised types are not.</li>
  40. <li>It actually turns out to be quite useful when writing generic code
  41. to pass around object constructing factory objects all of which have
  42. no parameters for their call operator. It becomes, effectively, a
  43. <em>lazy construction</em> mechanism.</li>
  44. </ol>
  45. </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
  46. <hr>
  47. <div class="spirit-nav">
  48. <a accesskey="p" href="../../../tutorial/advanced/constructors/metaprogrammg2.html"><img src="../../../images/prev.png" alt="Prev"></a>
  49. <a accesskey="u" href="../../../tutorial/advanced/constructors.html"><img src="../../../images/up.png" alt="Up"></a>
  50. <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
  51. </html>