Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a SFINAE-based solution that checks if a function-call expression yields an lvalue:</p> <pre><code>#include &lt;boost/type_traits.hpp&gt; #include &lt;boost/utility.hpp&gt; #include &lt;cstddef&gt; // Func: function (object/pointer/reference) type // Arg0: type of the first argument to use (for overload resolution) template&lt;class Func, class Arg0&gt; struct yields_lvalue_1 // with one argument { typedef char yes[1]; typedef char no[2]; // decay possible function types typedef typename boost::decay&lt;Func&gt;::type F_decayed; // a type whose constructor can take any lvalue expression struct Any { template&lt;class T&gt; Any(T&amp;); }; // SFINAE-test: if `Any(....)` is well-formed, this overload of `test` is // viable template&lt;class T&gt; static yes&amp; test(boost::integral_constant&lt;std::size_t, sizeof(Any( boost::declval&lt;T&gt;()(boost::declval&lt;Arg0&gt;()) ))&gt;*); // fall-back template&lt;class T&gt; static no&amp; test(...); // perform test static bool const result = sizeof(test&lt;F_decayed&gt;(0)) == sizeof(yes); }; </code></pre> <p>Some exemplary function objects:</p> <pre><code>struct foo { bool&amp; operator()(int); bool operator()(double); }; struct bar { template&lt;class T&gt; double operator()(T); }; </code></pre> <p>Usage example:</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; void print(bool expect, bool result) { std::cout &lt;&lt; "expect: "&lt;&lt;std::setw(5)&lt;&lt;expect&lt;&lt;" -- result: "&lt;&lt;result&lt;&lt;"\n"; } int main() { std::cout &lt;&lt; std::boolalpha; print(true , yields_lvalue_1&lt;foo, int&gt; ::result); print(false, yields_lvalue_1&lt;foo, double&gt;::result); print(false, yields_lvalue_1&lt;bar, int&gt; ::result); print(true , yields_lvalue_1&lt;foo&amp;(*)(long), int&gt;::result); print(false, yields_lvalue_1&lt;void(*)(int), short&gt;::result); print(true , yields_lvalue_1&lt;bool&amp;(short), long&gt;::result); print(false, yields_lvalue_1&lt;void(float), int&gt;::result); print(true , yields_lvalue_1&lt;char&amp;(&amp;)(bool), long&gt;::result); print(false, yields_lvalue_1&lt;foo(&amp;)(int), short&gt;::result); } </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. 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.
 

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