Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>#include &lt;iostream&gt; void MyFunction(float a){std::cout &lt;&lt; "float\n";} void MyFunction(int a){std::cout &lt;&lt; "int\n";} template&lt;typename Func, typename T&gt; void Do( Func f, T t ) { f(t); } template&lt;typename T&gt; void DoMyFunction( T t ) { Do(static_cast&lt;void(*)(T)&gt;(MyFunction), t); } template&lt;typename T&gt; void DoSomeFunction( T t, void(*func)(T) ) { Do(func, t); } int main() { float a; MyFunction(a); void (*manualFunctionPointer)(float) = MyFunction; manualFunctionPointer(a); // Do(MyFunction, a); -- does not compile Do(static_cast&lt;void(*)(float)&gt;(MyFunction), a); DoMyFunction(a); DoSomeFunction(a, MyFunction); } </code></pre> <p>The above works. I picked a MyFunction in 4 different ways.</p> <p>If you are willing to do some boilerlate, and want to solve the "what if a is a <code>char</code>" problem, then this might help:</p> <pre><code>// wrap the idea of calling MyFunction in a type: struct MyFunctionFunctor { template&lt;typename T&gt; static auto Do( T&amp;&amp; t )-&gt;decltype(MyFunction(std::forward(t))) { return MyFunction(std::forward(t)); } }; // Calling MyFunctionFunctor::Do( x ) will basically do static dispatch on all of // the overloads of MyFunction // wrap the idea of dispatching a variable to a functor: template&lt;typename T, typename Functor&gt; struct Dispatch { static auto Do( T t )-&gt;decltype( Functor::Do( t ) ) { return Functor::Do( t ); } } int main() { char a; auto func_ptr = &amp;Dispatch&lt;decltype(a), MyFunctionFunctor&gt;::Do; func_ptr(a); } </code></pre> <p>but, as noted, this requires we wrap our <code>MyFunction</code> up so that it is described by a type. I'm not aware of a way to do this without boilerplate.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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