Yet Another SFINAE tool in C++11

Posted by & filed under c++, c++11.

SFINAE in C++03 typically uses the sizeof operator to determine which overload for a function was valid, and hence whether the type under consideration has a particular property, usually a typedef. Here is the prototypical C++03 code for finding if a type T has a typedef foo inside it.

With C++11, we can use another unevaluated context, namely decltype to check for the return type of the function that was valid. The value variable can now be

There is yet another unevaluated context in C++11, namely the noexcept operator. The operator is passed an expression, and it returns true or false depending on whether the expression is expected NOT to throw an exception or IS expected to throw an exception, respectively. We can make use of that fact, and define the two versions of the test function to throw and not throw. Here is a version that uses the noexcept operator, and doesn’t have the helper yes/no types.

One Response to “Yet Another SFINAE tool in C++11”

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

*