There is a (seemingly) nice C++03 way of finding out if a type has a member function or operator:
https://github.com/jaredhoberock/is_call_possible/blob/master/is_call_possible.hpp
Is there no modern C++11 way to do this? It would be better not having to include any external code and use only the standard.
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-
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 {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With