Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way of renaming (alias/forward) a function in C++?
    primarykey
    data
    text
    <p>(I'll restrict this question to C++11, since I believe there is no general way to do this in C++98).</p> <p>Supposed I have a complicated (in terms of signature) set of <em>template</em> functions and/or overloaded functions, and I want to use these functions in the exact same way but using a different name (that is, an alias).</p> <p>For example:</p> <pre><code>template&lt;class A, class B, class C&gt; D fun(A a, B&amp; b, C&amp;&amp; c){ ... } template&lt;class E, class F&gt; G fun(H&lt;E&gt; he, F&amp; f){ ... } ... many other versions of fun </code></pre> <p>Now suppose that I want to <em>rename</em> (or <em>alias</em>, or <em>forward</em> to be more precise) these functions, all at once (i.e. be able to use a different name for the same function without rewriting it). Such that, in other parts of the code I can use it with a different name and without modifying the above code.</p> <p>Is this the correct way to <em>rename</em> (alias/forward) <code>fun</code> into <code>gun</code>?</p> <pre><code>template&lt;typename... Args&gt; inline auto gun(Args&amp;&amp;... args)-&gt;decltype(fun(std::forward&lt;Args&gt;(args)...)){ return fun(std::forward&lt;Args&gt;(args)...); } </code></pre> <ul> <li>Is it truly general?</li> <li>Is this the simplest way?</li> <li>Is this the optimal way? (e.g. can be inlined, no unnecessary copies)</li> <li>What if the original function had some SFINAE features? (e.g. <code>template&lt;class A, class B, class C, class = std::enable_if&lt; ... &gt;::type&gt;</code>), will <code>decltype</code> transfer the SFINAE in all cases?</li> <li>What if the original function returned references? Isn't the decltype going to remove the references type?. (e.g. <code>double&amp; fun(double&amp; x){return x;}</code>).</li> <li>Can same be said about member functions?</li> </ul> <p>Clarification: <code>gun</code> is never going to be <em>exactly</em> <code>fun</code>, as the instances will have different addresses, but what I am looking for is a rename for the point of view of generic coding. </p> <p>Comment: I find strange that almost everything can be renamed/forwarded, namespaces, types (<code>typedef</code>) and template types <code>using typedef</code>, but not functions (or for that matter member functions).</p> <hr> <p><strong>EDIT</strong>: For completeness, and since this seems to be the way to do it, here I added a macro to define function aliases:</p> <pre><code>#define ALIAS_FUNCTION(OriginalnamE, AliasnamE) \ template &lt;typename... Args&gt; \ inline auto AliasnamE(Args&amp;&amp;... args) -&gt; decltype(OriginalnamE(std::forward&lt;Args&gt;(args)...)) { \ return OriginalnamE(std::forward&lt;Args&gt;(args)...); \ } </code></pre> <p>and then you use it like this:</p> <pre><code>namespace NS1{namepsace NS2{ ALIAS_FUNCTION(NSA::fun, gun); // second argument (target name can't have namespace) }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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