Note that there are some explanatory texts on larger screens.

plurals
  1. POFunctions and functors as arguments to template functions
    primarykey
    data
    text
    <p>I'm looking for a way to pass function pointers, functors or lambdas to a template function <code>g</code> which uses the passed function's argument types, for example:</p> <pre><code>template&lt;class T1, class T2, class T3&gt; struct wrapper_t { boost::function&lt;void(T1,T2,T3)&gt; f; wrapper_t( boost::function&lt;void(T1,T2,T3)&gt; f ) : f(f) {} void operator( std::vector&lt;T1&gt; &amp;a, std::vector&lt;T2&gt; &amp;b, T3 c ) { assert(a.size() == b.size()); for(size_t i = 0 ; i != a.size() ; i++) f(a[i], b[i], c); } }; template&lt;class T1, class T2, class T3&gt; wrapper_t&lt;T1,T2,T3&gt; make_wrapper( boost::function&lt;void(T1,T2,T3)&gt; f ) { return wrapper_t&lt;T1,T2,T3&gt;( f ); } void f(int, double, char) {}; wrapper_t&lt;int, double, char&gt; w0(f); // need to repeat types auto w1 = make_wrapper(f); // more comfortable std::vector&lt;int&gt; a{{1, 2, 3}}; std::vector&lt;double&gt; b{{1.0, 2.0, 3.0}}; w0( a, b, 'c' ); w1( a, b, 'c' ); </code></pre> <p>The <code>make_wrapper</code> function only exists to extract the types from the argument, some syntactic sugar to avoid having to type them twice.</p> <hr> <p>A minimal example for my problem is this function:</p> <pre><code>template&lt;class T&gt; void g1( const boost::function&lt;void(T)&gt; &amp; ) {} </code></pre> <p>Using these as input</p> <pre><code>void f1(int) {} struct f2_t { void operator()(int) {} }; </code></pre> <p>it fails to infer <code>T=int</code></p> <pre><code>f2_t f2; g1( f1 ); // mismatched types ‘const std::function&lt;void(T)&gt;’ and ‘void(int)’ g1( f2 ); // ‘f2_t’ is not derived from ‘const std::function&lt;void(T)&gt;’ g1( [](int){} ); // ‘::&lt;lambda(int)&gt;’ is not derived from ‘… g1&lt;int&gt;( f1 ); // ok g1&lt;int&gt;( f2 ); // ok g1&lt;int&gt;( [](int){} ); // ok </code></pre> <p>But <code>T=int</code> can be inferred from a plain function pointer, but of this doesn't work with the functor or lambda either:</p> <pre><code>template&lt;class T&gt; void g2( void (*)(T) ) {} g2( f1 ); // ok g2( f2 ); // mismatched types … g2&lt;int&gt;( f2 ); // ok g2( [](int){} ); // mismatched types … g2&lt;int&gt;( [](int){} ); // ok </code></pre> <p><em>Is there a way to infer <code>T</code> not just for plain function pointers but for functors and lambdas, too?</em></p> <p>Or does it have to be something like this?</p> <pre><code>template&lt;class F&gt; void g( F ) { typedef first_argument_of&lt;F&gt;::type T; } </code></pre> <p>(in my real code I need to deconstruct a function with four arguments this way, but <code>std::function::…argument_type</code> only exists for one or two arguments; <code>boost::function</code> has argN_type, but I don't think I can use that anyway since <code>F</code> is not always a <code>function</code> which is my problem, see above, etc)</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.
 

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