Puppy
Puppy

Reputation: 146910

Lambdas can't be compared

I was wondering if this is Standard, or a bug in my code. I'm trying to compare a pair of my homegrown function objects. I rejected the comparison if the type of function object is not the same, so I know that the two lambdas are the same type. So why can't they be compared?

Upvotes: 2

Views: 163

Answers (2)

Anthony Williams
Anthony Williams

Reputation: 68571

Every C++0x lambda object has a distinct type, even if the signature is the same.

auto l1=[](){}; // one do-nothing lambda
auto l2=[](){}; // and another
l1=l2; // ERROR: l1 and l2 have distinct types

If two C++0x lambdas have the same type, they must therefore have come from the same line of code. Of course, if they capture variables then they won't necessarily be identical, as they may have come from different invocations.

However, a C++0x lambda does not have any comparison operators, so you cannot compare instances to see if they are indeed the same, or just the same type. This makes sense when you think about it: if the captured variables do not have comparison operators then you cannot compare lambdas of that type, since each copy may have different values for the captured variables.

Upvotes: 3

Achilles
Achilles

Reputation: 11299

Is the equality operator overloaded for your lambda object? If not I'm assuming you'll need to implement it.

Upvotes: 0

Related Questions