Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Imagine you have the following generic function (all it does is pass arguments (called "forwarding") to the inner function <code>callme</code>):</p> <pre><code>template &lt;typename T&gt; void f(T&amp; t) { callme(t); } </code></pre> <p>Now, pretend that the definition of <code>callme</code> looks like this: <code>void callme(const int&amp; i)</code>. The problem is that <code>callme(1)</code> will work, but <code>f(1)</code> will not (this is because rvalues do not bind to non-const references). To solve this, you need to change <code>f</code> to: <code>f(const T&amp; t)</code>. However, this introduces a new problem. If <code>callme</code> is defined like this: <code>void callme(int&amp; i)</code>, you will get a compile error. The solution is to make two overloads for <code>f</code> to take both <code>f(const T&amp; t)</code> and <code>f(T&amp; t)</code>.</p> <p>Now, the above situation works fine when a function only takes a single argument. However, imagine a function that takes two arguments, you have to make 4 overloads:</p> <pre><code>f(const T1&amp;, const T2&amp;) f(const T1&amp;, T2&amp;) f(T1&amp;, const T2&amp;) f(T1&amp;, T2&amp;) </code></pre> <p><a href="http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx" rel="nofollow">In this article on msdn</a>, it mentions that for <code>tr1::bind</code>, for 5 arguments, a total of 63 overloads was needed! What I have just explained is called the <a href="http://www.google.co.jp/search?q=forwarding+problem&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla%3aja%3aofficial&amp;hl=ja&amp;client=firefox-a" rel="nofollow">forwarding problem</a> which has been solved in <code>C++11</code> using perfect forwarding and rvalue references.</p> <p>Quote from Boost Site:</p> <p><code>The library chooses a small subset: for up to two arguments, it provides the const overloads in full, for arities of three and more it provides a single additional overload with all of the arguments taken by const reference.</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload