Я хочу написать метафункцию C ++ is_callable
, которая определяет значение
как истинное
, если и только если тип F имеет оператор вызова функции вида SomeReturnType operator () (const Arg &)
. Например, в следующем случае
struct foo {
void operator(const int &) {}
};
я хочу is_callable
должно быть ложным
и is_callable
должно быть истинным
. Вот что у меня есть:
#include <memory>
#include <iostream>
template<typename F, typename Arg>
struct is_callable {
private:
template<typename>
static char (&test(...))[2];
template<unsigned>
struct helper {
typedef void *type;
};
template<typename UVisitor>
static char test(
typename helper<
sizeof(std::declval<UVisitor>()(std::declval<Arg>()), 0)
>::type
);
public:
static const bool value = (sizeof(test<F>(0)) == sizeof(char));
};
struct foo {
void operator()(const int &) {}
};
using namespace std;
int main(void)
{
cout << is_callable<foo, int &>::value << "\n";
cout << is_callable<foo, const int &>::value << "\n";
return 0;
}
Это печатает ] 1
и 1
, но я хочу 0
и 1
, потому что foo
определяет только void operator () (const int &)
.