本文介绍了是一个C ++ is_lambda trait,纯粹实现为库,不可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个关于C ++ 0x lambdas的问题。在我的代码中,知道给定类型是否是C ++ 0x lambda表达式的类型是有益的。例如:

I have a question regarding C++0x lambdas. In my code, it would be beneficial to know whether or not a given type is the type of a C++0x lambda expression. To give an example:

struct foobar
{
  void operator()()
  {
  }
};

auto lambda = []{};
typedef is_lambda < decltype(lambda) > ::type T; // T would be a true_type
typedef is_lambda < foobar > ::type T; // T would be a false_type

很容易区分lambda表达式和函数和成员函数类型。函数是另一回事。

It is rather easy to distinguish lambda expressions from function and member function types. Functors are another matter.

我看到的问题是根据即将到来的C ++ 0x标准定义lambda表达式;唯一必须定义的是公共调用操作符。然而,这也适用于函子;测试调用操作符的存在不足以区分lambda表达式和函子。此外,如果函子的运算符不存在,则会出现编译器错误,因为SFINAE不适用。什么时候发生?函子的调用操作符可以是模板化的。
所以,这样的代码:

The problem I see here is the definition of lambda expressions according to the upcoming C++0x standard; the only thing that must be defined is a public call operator. However, this is true for a functor as well; testing for the presence of the call operator is not enough for distinguishing lambda expressions from functors. Furthermore, if the operator of a functor is not present, a compiler error will occur, since SFINAE does not apply. When does this happen? The functor's call operator may be templated.So, such a code:

typedef decltype(&T::operator()) call_type;

将同时用于lambda表达式和带有非模板化调用操作符的函子,并生成编译器错误模板调用操作符。

will work for both lambda expressions and functors with non-templated call operator, and generate a compiler error for templated call operators.

我相信一个 is_lambda< > trait只能使用内部编译器功能创建。

I believe an is_lambda < > trait can only be created using intrinsic compiler features. Do you see a way how to implement this trait?

推荐答案

由于对lambda的求值导致创建闭包对象,因此不存在任何差异一旦对象传递给函数或复制。坦率地说,我不能想象一个问题,需要知道一个对象是否来自lambda。

Since evaluation of lambda results in creating closure object, there isn't any difference as soon as the object passed to a function or copied. And, frankly, I can't imagine a problem that would require to know whether an object came from lambda.

编辑。标准甚至在5.1.2 / 2中有一个注释:

Edit. A standard even has a note in 5.1.2/2:

这篇关于是一个C ++ is_lambda trait,纯粹实现为库,不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:48