Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecializing a template on a lambda in C++0x
    primarykey
    data
    text
    <p>I've written a traits class that lets me extract information about the arguments and type of a function or function object in C++0x (tested with gcc 4.5.0). The general case handles function objects:</p> <pre><code>template &lt;typename F&gt; struct function_traits { template &lt;typename R, typename... A&gt; struct _internal { }; template &lt;typename R, typename... A&gt; struct _internal&lt;R (F::*)(A...)&gt; { // ... }; typedef typename _internal&lt;decltype(&amp;F::operator())&gt;::&lt;&lt;nested types go here&gt;&gt;; }; </code></pre> <p>Then I have a specialization for plain functions at global scope:</p> <pre><code>template &lt;typename R, typename... A&gt; struct function_traits&lt;R (*)(A...)&gt; { // ... }; </code></pre> <p>This works fine, I can pass a function into the template or a function object and it works properly:</p> <pre><code>template &lt;typename F&gt; void foo(F f) { typename function_traits&lt;F&gt;::whatever ...; } int f(int x) { ... } foo(f); </code></pre> <p>What if, instead of passing a function or function object into <code>foo</code>, I want to pass a lambda expression?</p> <pre><code>foo([](int x) { ... }); </code></pre> <p>The problem here is that neither specialization of <code>function_traits&lt;&gt;</code> applies. The C++0x draft says that the type of the expression is a "unique, unnamed, non-union class type". Demangling the result of calling <code>typeid(...).name()</code> on the expression gives me what appears to be gcc's internal naming convention for the lambda, <code>main::{lambda(int)#1}</code>, not something that syntactically represents a C++ typename.</p> <p>In short, is there anything I can put into the template here:</p> <pre><code>template &lt;typename R, typename... A&gt; struct function_traits&lt;????&gt; { ... } </code></pre> <p>that will allow this traits class to accept a lambda expression?</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.
 

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