型 F が形式の関数呼び出し演算子を持っている場合にのみ、beis_callable<F, Arg>
と定義する C++ メタ関数を書きたいと思います。たとえば、次の場合value
true
SomeReturnType operator()(const Arg &)
struct foo {
void operator(const int &) {}
};
私はなりたいとis_callable<foo, int &>
思います。これは私がこれまでに持っているものです:false
is_callable<foo, const int &>
true
#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
andを出力しますが、 andのみが定義されているため、 and1
が必要です。0
1
foo
void operator()(const int &)