Is a C++ is_lambda trait, purely implemented as a library, impossible?

霸气de小男生 提交于 2019-12-18 05:45:12

问题


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

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

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;

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

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


回答1:


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.

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

Note: a closure object behaves like a function object (20.8).—end note




回答2:


I don't believe that it can be done- lambdas aren't really anything new semantically, they're just compiler-generated functors and thus will look identical to regular functors.




回答3:


It's possible to define some macro code that determines if an expression is a lambda expression (but that's not very useful as it doesn't tell you if an expression is of a lambda type).

#include <type_traits>

template<typename T, typename U>
struct SameType {
    static_assert(!std::is_same<T, U>::value, "Must use Lambda");
    static T pass(T t) { return t; }
};

template <typename T, typename U>
T NotLambda(T t, U u) { return SameType<T, U>::pass(t); }

#define ASSERT_LAMBDA(x) NotLambda(x,x)

/////////////////////////////////////

int fn() { return 0; }

int main() {
    auto l = []{ return 0; };
    return ASSERT_LAMBDA(fn)() +             // << fails
           ASSERT_LAMBDA(l)() +              // << fails
           ASSERT_LAMBDA([]{ return 0; })(); // << passes
}

This depends on section 5.1.2.3 which specifies that each lambda expression has a distinct type (which I think is a property unique to lambdas).



来源:https://stackoverflow.com/questions/4661875/is-a-c-is-lambda-trait-purely-implemented-as-a-library-impossible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!