I want to write a C++ metafunction is_callable
that defines value
to be true
, if and only if the type F has the function cal
Ran across this while doing something else, was able to adapt my code to fit. It has the same features (and limitations) as @Xeo, but does not need sizeof trick/enable_if. The default parameter takes the place of needing to do the enable_if to handle template functions. I tested it under g++ 4.7 and clang 3.2 using the same test code Xeo wrote up
#include
#include
namespace detail {
template
struct call_exact : std::false_type {};
template struct ARGS { typedef void type; };
template
C * opclass(decltype(std::declval()(std::declval()...)) (C::*)(Args...)) { }
template
C * opclass(decltype(std::declval()(std::declval()...)) (C::*)(Args...) const) { }
template
struct call_exact,
typename ARGS<
decltype(std::declval()(std::declval()...)),
decltype(opclass(&T::operator()))
>::type
> : std::true_type {};
}
template
struct Callable : detail::call_exact> { };
template
struct Callable
: Callable, Args...>{};