Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is possible to specialize traits for lambdas and do pattern matching on the signature of the unnamed functor. Here is the code that works on g++ 4.5. Although it works, the pattern matching on lambda appears to be working contrary to the intuition. I've comments inline.</p> <pre><code>struct X { float operator () (float i) { return i*2; } // If the following is enabled, program fails to compile // mostly because of ambiguity reasons. //double operator () (float i, double d) { return d*f; } }; template &lt;typename T&gt; struct function_traits // matches when T=X or T=lambda // As expected, lambda creates a "unique, unnamed, non-union class type" // so it matches here { // Here is what you are looking for. The type of the member operator() // of the lambda is taken and mapped again on function_traits. typedef typename function_traits&lt;decltype(&amp;T::operator())&gt;::return_type return_type; }; // matches for X::operator() but not of lambda::operator() template &lt;typename R, typename C, typename... A&gt; struct function_traits&lt;R (C::*)(A...)&gt; { typedef R return_type; }; // I initially thought the above defined member function specialization of // the trait will match lambdas::operator() because a lambda is a functor. // It does not, however. Instead, it matches the one below. // I wonder why? implementation defined? template &lt;typename R, typename... A&gt; struct function_traits&lt;R (*)(A...)&gt; // matches for lambda::operator() { typedef R return_type; }; template &lt;typename F&gt; typename function_traits&lt;F&gt;::return_type foo(F f) { return f(10); } template &lt;typename F&gt; typename function_traits&lt;F&gt;::return_type bar(F f) { return f(5.0f, 100, 0.34); } int f(int x) { return x + x; } int main(void) { foo(f); foo(X()); bar([](float f, int l, double d){ return f+l+d; }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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