可以通过访问operator()
来推断非泛型lambda的arity。
template <typename F>
struct fInfo : fInfo<decltype(&F::operator())> { };
template <typename F, typename Ret, typename... Args>
struct fInfo<Ret(F::*)(Args...)const> { static const int arity = sizeof...(Args); };
对[](int x){ return x; }
之类的内容来说,这很好用,因为operator()
没有模板化。
但是,通用lambda会对operator()
进行模板化,并且只能访问模板的具体实例 - 这有点问题,因为我无法手动为operator()
提供模板参数我不知道它的本质是什么。
当然,像
这样的东西auto lambda = [](auto x){ return x; };
auto arity = fInfo<decltype(lambda)>::arity;
不起作用。
我不知道要投射什么,也不知道提供哪些模板参数(或多少)(operator()<??>
)。
任何想法如何做到这一点?
答案 0 :(得分:6)
这种技术在某些情况下会起作用。我创建了一个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
参数等
但是,如果lambda使用->X
子句指定其返回值,那么fake_anything
就足够了。困难的部分是处理身体。
请注意,这种方法通常是一个坏主意,因为如果您想知道函数的arity,您可能也知道要调用函数对象的事物的类型!上面我很容易回答这个问题(可以用这些参数调用这个函数对象吗?)。甚至可以改进来询问“这些参数的最长/最短前缀是什么,可以调用这个函数对象”,或者处理“有多少次类型X重复调用这个函数对象”(如果你想干净失败,你需要一个上限)。
答案 1 :(得分:2)
这是不可能的,因为函数调用操作符可以是可变参数模板。对于一般的功能对象来说永远不可能永远这样做,而特殊套管lambdas因为它们碰巧不是同样强大而总是一个坏主意。现在是时候让这个坏主意回家了。
答案 2 :(得分:1)
我会说这是部分可能,当你明确地实例化{{1的自动参数时,至少你可以知道整体arity (模板化+常规类型) }}:
operator()
答案 3 :(得分:1)
这是一个c ++ 17解决方案,适用于具有可变参数templatet operator()的泛型和可变参数lambda和仿函数。我们的想法是以递减的参数数量递归模拟调用,并在找到第一个匹配数量的参数时使用SFINAE来中断递归。它编译器在gcc&gt; = 7和Clang&gt; = 5。可以找到一个工作示例here。
#include<utility>
constexpr size_t max_arity = 10;
struct variadic_t
{
};
namespace detail
{
// it is templated, to be able to create a
// "sequence" of arbitrary_t's of given size and
// hece, to 'simulate' an arbitrary function signature.
template <size_t>
struct arbitrary_t
{
// this type casts implicitly to anything,
// thus, it can represent an arbitrary type.
template <typename T>
operator T &&();
template <typename T>
operator T &();
};
template <typename F, size_t... Is,
typename U = decltype(std::declval<F>()(arbitrary_t<Is>{}...))>
constexpr auto test_signature(std::index_sequence<Is...>)
{
return std::integral_constant<size_t, sizeof...(Is)>{};
}
template <size_t I, typename F>
constexpr auto arity_impl(int) -> decltype(test_signature<F>(std::make_index_sequence<I>{}))
{
return {};
}
template <size_t I, typename F, typename = std::enable_if_t<(I > 0)>>
constexpr auto arity_impl(...)
{
// try the int overload which will only work,
// if F takes I-1 arguments. Otherwise this
// overload will be selected and we'll try it
// with one element less.
return arity_impl<I - 1, F>(0);
}
template <typename F, size_t MaxArity = 10>
constexpr auto arity_impl()
{
// start checking function signatures with max_arity + 1 elements
constexpr auto tmp = arity_impl<MaxArity + 1, F>(0);
if constexpr (tmp == MaxArity + 1)
{
// if that works, F is considered variadic
return variadic_t{};
}
else
{
// if not, tmp will be the correct arity of F
return tmp;
}
}
}
template <typename F, size_t MaxArity = max_arity>
constexpr auto arity(F&& f) { return detail::arity_impl<std::decay_t<F>, MaxArity>(); }
template <typename F, size_t MaxArity = max_arity>
constexpr auto arity_v = detail::arity_impl<std::decay_t<F>, MaxArity>();
template <typename F, size_t MaxArity = max_arity>
constexpr bool is_variadic_v = std::is_same_v<std::decay_t<decltype(arity_v<F, MaxArity>)>, variadic_t>;
用法:
auto l = [](auto...){};
static_assert(is_variadic_v<decltype(l)>);
和
auto l = [](auto, auto, auto){};
static_assert(!is_variadic_v<decltype(l)>);
static_assert(arity(l) == 3);
答案 4 :(得分:0)
另一种可行的解决方案,适用于已知模板类型的情况: http://coliru.stacked-crooked.com/a/e3a07d723a8f27e9
using T1 = string;
using T2 = int;
std::integral_constant<int, 1> static arity(function<void(T1)>){ return {}; }
std::integral_constant<int, 2> static arity(function<void(T1, T2)>){ return {}; }
template<class Fn>
using Arity = decltype(arity(Fn{}));