Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is lambda comparison deterministic?

Tags:

c++

c++11

lambda

As we know, comparing two matching string literals can result in equality:

"hello" == "hello" //could be true or false

Does the same hold for lambdas:

[](){} == [](){} //false - guaranteed?

Is the compiler free to evaluate this as it pleases, or is it guaranteed that it will evaluate to false? Is it legal? What does the above actually compare?

like image 862
Luchian Grigore Avatar asked Dec 20 '12 11:12

Luchian Grigore


1 Answers

You cannot compare lambdas for equality. What you see in that little snippet is the lambdas being converted to function pointers and then the function pointers are compared. There are no guarantees that those function pointers are or are not the same, which means the result can be either true or false.

like image 187
R. Martinho Fernandes Avatar answered Nov 04 '22 05:11

R. Martinho Fernandes