Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note: These assume that the default constructor is valid for the type your checking. Not sure offhand how to get around that.</p> <p>The following seems to work if it's callable with 0 arguments. Is there something in is_function's implementation that might help to extend this to 1 or more argument callables?:</p> <pre><code>template &lt;typename T&gt; struct is_callable { // Types "yes" and "no" are guaranteed to have different sizes, // specifically sizeof(yes) == 1 and sizeof(no) == 2. typedef char yes[1]; typedef char no[2]; template &lt;typename C&gt; static yes&amp; test(decltype(C()())*); template &lt;typename&gt; static no&amp; test(...); // If the "sizeof" the result of calling test&lt;T&gt;(0) would be equal to the sizeof(yes), // the first overload worked and T has a nested type named foobar. static const bool value = sizeof(test&lt;T&gt;(0)) == sizeof(yes); }; </code></pre> <p>If you know the type of the argument (even if it's a template parameter), the following would work for 1 argument, and I imagine one could extend pretty easily from there:</p> <pre><code>template &lt;typename T, typename T2&gt; struct is_callable_1 { // Types "yes" and "no" are guaranteed to have different sizes, // specifically sizeof(yes) == 1 and sizeof(no) == 2. typedef char yes[1]; typedef char no[2]; template &lt;typename C&gt; static yes&amp; test(decltype(C()(T2()))*); template &lt;typename, typename&gt; static no&amp; test(...); // If the "sizeof" the result of calling test&lt;T&gt;(0) would be equal to the sizeof(yes), // the first overload worked and T has a nested type named foobar. static const bool value = sizeof(test&lt;T&gt;(0)) == sizeof(yes); }; </code></pre> <p><strong>Edit</strong> <a href="http://pastebin.com/i57SEY7a" rel="nofollow">here</a> is a modification that handles the case where default constructor isn't available.</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.
 

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