Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a really interesting question. I've been puzzled hard by it.</p> <p>I think I managed to make a variation to Crazy Eddie's code that will allow any number of parameters, however, it does use variadic templates and it does require to specify the parameters you are expecting the "callable" object to be called with. Long story short, I got this running and working as expected on gcc 4.6.0:</p> <p><strong>EDIT:</strong> One could use the std::result_of utility as well, however it does not work because it requires a <code>typename</code> to disambiguate the <code>std::result_of&lt;..&gt;::type</code> which breaks the Sfinae.</p> <pre><code>#include &lt;iostream&gt; #include &lt;type_traits&gt; template &lt; typename PotentiallyCallable, typename... Args&gt; struct is_callable { typedef char (&amp;no) [1]; typedef char (&amp;yes) [2]; template &lt; typename T &gt; struct dummy; template &lt; typename CheckType&gt; static yes check(dummy&lt;decltype(std::declval&lt;CheckType&gt;()(std::declval&lt;Args&gt;()...))&gt; *); template &lt; typename CheckType&gt; static no check(...); enum { value = sizeof(check&lt;PotentiallyCallable&gt;(0)) == sizeof(yes) }; }; int f1(int,double) { return 0; }; typedef int(*f1_type)(int,double) ; //this is just to have a type to feed the template. struct Foo { }; struct Bar { template &lt;typename T&gt; void operator()(T) { }; }; int main() { if( is_callable&lt;f1_type,int,double&gt;::value ) std::cout &lt;&lt; "f1 is callable!" &lt;&lt; std::endl; if( is_callable&lt;Foo&gt;::value ) std::cout &lt;&lt; "Foo is callable!" &lt;&lt; std::endl; if( is_callable&lt;Bar,int&gt;::value ) std::cout &lt;&lt; "Bar is callable with int!" &lt;&lt; std::endl; if( is_callable&lt;Bar,double&gt;::value ) std::cout &lt;&lt; "Bar is callable with double!" &lt;&lt; std::endl; }; </code></pre> <p>I hope this is what you are looking for because I don't think it is possible to do much more.</p> <p><strong>EDIT:</strong> For your use case, it is a partial solution, but it might help:</p> <pre><code>template &lt;typename Predicate1, typename Predicate2&gt; struct and_predicate { template &lt;typename ArgT&gt; enable_if&lt;ice_and&lt;is_callable&lt;Predicate1,ArgT&gt;::value, is_callable&lt;Predicate2,ArgT&gt;::value&gt;::value, bool&gt;::type operator()(const ArgT&amp; arg) { return predicate1(arg) &amp;&amp; predicate2(arg); } Predicate1 predicate1; Predicate2 predicate2; }; template &lt;typename Predicate1, typename Predicate2&gt; enable_if&lt;ice_and&lt;is_callable&lt; Predicate1, boost::any &gt;::value, is_callable&lt; Predicate2, boost::any &gt;::value&gt;::value, and_predicate&lt;Predicate1, Predicate2&gt;&gt;::type operator&amp;&amp;(Predicate1 predicate1, Predicate2 predicate2) { return and_predicate&lt;Predicate1, Predicate2&gt;{predicate1, predicate2}; } </code></pre>
    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.
    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