通用lambda的Arity [英] Arity of a generic lambda

查看:69
本文介绍了通用lambda的Arity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过访问其 operator()来推断非通用lambda的奇偶性。

It is possible to deduce arity of a non-generic lambda by accessing its operator().

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){返回x; } ,因为 operator()未模板化。

This is nice and dandy for something like [](int x){ return x; } as the operator() is not templated.

但是,通用lambda可以 operator()的模板,并且只能访问模板的具体实例-这有点问题,因为我不能手动提供<$ c的模板参数$ c> operator(),因为我不知道它的含义是什么。

However, generic lambdas do template the operator() and it is only possible to access a concrete instantiation of the template - which is slightly problematic because I can't manually provide template arguments for the operator() as I don't know what its arity is.

所以,当然,类似

auto lambda = [](auto x){ return x; };
auto arity = fInfo<decltype(lambda)>::arity;

不起作用。

I不知道要强制转换为什么,也不知道要提供什么模板参数(或多少个)( operator()< ??> )。

关于如何执行此操作的任何想法?

I don't know what to cast to nor do I know what template arguments to provide (or how many) (operator()<??>).
Any ideas how to do this?

推荐答案

这是不可能的,因为函数调用运算符可以是可变参数模板。一般而言,永远不可能对功能对象执行此操作,而特殊外壳的lambda,因为它们恰好没有同样强大的功能,总是一个坏主意。现在该是个坏主意回家的时候了。

It's impossible, as the function call operator can be a variadic template. It's been impossible to do this forever for function objects in general, and special-casing lambdas because they happened to not be equally powerful was always going to be a bad idea. Now it's just time for that bad idea to come home to roost.

这篇关于通用lambda的Arity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆