How to determine if a type is a lambda expression at compile time?

Suppose I have a type my_structcontaining a member variable f, which is a function. Possibly what fis the lambda function of C ++ 11.

Since assigning lambda objects is illegal, I would like to implement an assignment operator my_structsuch that when it fis a lambda, it is not assigned.

Is it possible to build a type trait is_lambdathat can check the type for a lambda error?

In code:

#include <type_traits>

template<typename Function> struct is_lambda
{
  // what goes here?
};

template<typename Function> struct my_struct
{
  Function f;

  my_struct &do_assign(const my_struct &other, std::true_type)
  {
    // don't assign to f
    return *this;
  }

  my_struct &do_assign(const my_struct &other, std::false_type)
  {
    // do assign to f
    f = other.f;
    return *this;
  }

  my_struct &operator=(const my_struct &other)
  {
    return do_assign(other, typename is_lambda<Function>::type());
  }
};
+5
source share
2 answers

, - , .

§5.1.2 [expr.prim.lambda] p3

- ( ) , ununion class [...]

+7

, --, std::is_assignable.

+4

All Articles