Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do lambdas have different types?

Tags:

c++

Every lambda should have a unique unknown type.. is it guaranteed that two lambdas into the same scope have different types?

int main() {
   auto l1 = [](){};
   auto l2 = [](){};   
  static_assert(std::is_same<decltype(l1), decltype(l2)>::value == false, "Types cannot be equal!");
}

This works but I'd like to know if it's guaranteed that the assert will never fire.

like image 459
Dean Avatar asked Jan 04 '16 17:01

Dean


People also ask

How many types of lambda are there?

1) Argument-list: It can be empty or non-empty as well. 2) Arrow-token: It is used to link arguments-list and body of expression. 3) Body: It contains expressions and statements for lambda expression.

What is the type of a lambda?

A type lambda lets one express a higher-kinded type directly, without a type definition. For instance, the type above defines a binary type constructor, which maps arguments X and Y to Map[Y, X] . Type parameters of type lambdas can have bounds, but they cannot carry + or - variance annotations.

Why are lambdas called lambdas?

But what is a Lambda expression? The term “Lambda” comes from mathematics, where it's called lambda calculus. In programming, a Lambda expression (or function) is just an anonymous function, i.e., a function with no name.

Which is a valid type for this lambda function?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a lambda expression. The RemoveHandler statement is an exception.


1 Answers

Yes, each lambda expression produces a unique type ([expr.prim.lambda]/3):

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below.

[emphasis added]

like image 130
Jerry Coffin Avatar answered Oct 18 '22 03:10

Jerry Coffin