这种技术在某些情况下会起作用。我创建了一个fake_anything
几乎可以伪造任何东西的类型,并尝试使用一些实例来调用您的 lambda。
#include <iostream>
struct fake_anything {
fake_anything(fake_anything const&);
fake_anything();
fake_anything&operator=(fake_anything const&);
template<class T>operator T&() const;
template<class T>operator T&&() const;
template<class T>operator T const&() const;
template<class T>operator T const&&() const;
fake_anything operator*() const;
fake_anything operator++() const;
fake_anything operator++(int) const;
fake_anything operator->() const;
template<class T>fake_anything(T&&);
};
fake_anything operator+(fake_anything, fake_anything);
fake_anything operator-(fake_anything, fake_anything);
fake_anything operator*(fake_anything, fake_anything);
fake_anything operator/(fake_anything, fake_anything);
// etc for every operator
template<class>using void_t=void;
template<class Sig, class=void>
struct can_invoke:std::false_type{};
template<class F, class...Args>
struct can_invoke<F(Args...),
void_t< decltype( std::declval<F>()( std::declval<Args>()... ) ) >
> : std::true_type
{};
template<class Sig>struct is_sig:std::false_type{};
template<class R, class...Args>struct is_sig<R(Args...)>:std::true_type{};
template<unsigned...>struct indexes{using type=indexes;};
template<unsigned Max,unsigned...Is>struct make_indexes:make_indexes<Max-1,Max-1,Is...>{};
template<unsigned...Is>struct make_indexes<0,Is...>:indexes<Is...>{};
template<unsigned max>using make_indexes_t=typename make_indexes<max>::type;
template<class T,unsigned>using unpacker=T;
template<class F, class A, class indexes>
struct nary_help;
template<class F, class A, unsigned...Is>
struct nary_help<F,A,indexes<Is...>>:
can_invoke<F( unpacker<A,Is>... )>
{};
template<class F, unsigned N>
struct has_n_arity:
nary_help<F, fake_anything, make_indexes_t<N>>
{};
template<class F, unsigned Min=0, unsigned Max=10>
struct max_arity{
enum{Mid=(Max+Min)/2};
enum{
lhs = max_arity<F,Min,Mid>::value,
rhs = max_arity<F,Mid+1,Max>::value,
value = lhs>rhs?lhs:rhs,
};
};
template<class F, unsigned X>
struct max_arity<F,X,X>:
std::integral_constant<int, has_n_arity<F,X>::value?(int)X:-1>
{};
template<class F, unsigned Min=0, unsigned Max=10>
struct min_arity{
enum{Mid=(Max+Min)/2};
enum{
lhs = min_arity<F,Min,Mid>::value,
rhs = min_arity<F,Mid+1,Max>::value,
value = lhs<rhs?lhs:rhs,
};
};
template<class F, unsigned X>
struct min_arity<F,X,X>:
std::integral_constant<unsigned,has_n_arity<F,X>::value?X:(unsigned)-1>
{};
auto test1 = [](auto x, auto y)->bool { return x < y; };
auto test2 = [](auto x, auto y) { return x + y; };
auto test3 = [](auto x) { return x.y; };
int main() {
std::cout << can_invoke< decltype(test1)( fake_anything, fake_anything ) >::value << "\n";
std::cout << can_invoke< decltype(test1)( int, int ) >::value << "\n";
std::cout << has_n_arity< decltype(test1), 2 >::value << "\n";
std::cout << max_arity< decltype(test1) >::value << "\n";
std::cout << max_arity< decltype(test2) >::value << "\n";
// will fail to compile:
// std::cout << max_arity< decltype(test3) >::value << "\n";
}
活生生的例子。
请注意,足够的 SFINAE 将意味着上述内容将得到错误的结果,使用operator.
或operator.
在某些“派生”类型上使用或使用基于fake_anything
参数的类型等也会得到错误的结果。
->X
但是,如果 lambda 使用子句指定其返回值,那么fake_anything
就足够了。最难的部分是处理身体。
请注意,这种方法通常不是一个好主意,因为如果您想知道函数的数量,您可能还知道要调用函数对象的事物的类型!上面我很容易回答这个问题(这个函数对象可以用这些参数调用吗?)。甚至可以改进询问“可以调用此函数对象的这些参数的最长/最短前缀是什么”,或处理“调用此函数对象的 X 类型重复次数”(如果您想要彻底失败,您需要一个上限)。