C++11 ways of finding if a type has member function or supports operator?

This works with all test cases given in the GitHub (Demo: http://ideone.com/ZLGp4R):

#include <type_traits>

template <typename C, typename F, typename = void>
struct is_call_possible : public std::false_type {};

template <typename C, typename R, typename... A>
struct is_call_possible<C, R(A...),
    typename std::enable_if<
        std::is_same<R, void>::value ||
        std::is_convertible<decltype(
            std::declval<C>().operator()(std::declval<A>()...)
            //                ^^^^^^^^^^ replace this with the member you need.
        ), R>::value
    >::type
> : public std::true_type {};

C++ 11 adds a new trick, which I often jokingly call "CFINAE" (compilation failure is not an error).

It makes use of the decltype operator and the regular properties of SFINAE.

Consider the following function:

template <typename X, typename Y>
static auto check(X& x, Y& y) -> decltype(x >> y);

It will be considered during overloading only if X and Y are types for which the shift operator is defined. Add a regular catch-all overload for check and you have a mechanism to test whether an arbitrary expression can be compiled.

And indeed, this is the principle developed in the experimental Origin library by Andrew Sutton (one of the authors of the Concepts Lite proposal). In fact, my example is taken straight from here to implement the Streamable concept.

I recommend the following presentation from GoingNative 2012 by Andrew Sutton and Bjarne Stroustrup where they give an introduction to the new take on concepts and the Origin library:

http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/A-Concept-Design-for-C-


No, it's pretty much the same way. More or less. Implementations vary, though you could replace some of the metafunctions used internally by that implementation with standard library traits. But there's no simple way of detecting if you can invoke a function on some type given some set of arguments.

That's for concepts (PDF).

Tags:

C++

C++11