I've bumped into a problem yesterday, which I eventually distilled into the following minimal example.
#include <iostream>
#include <functional>
int main()
{
int i=0, j=0;
std::cout
<< (&i == &j)
<< std::less<int *>()(&i, &j)
<< std::less<int *>()(&j, &i)
<< std::endl;
}
This particular program, when compiled using MSVC 9.0 with optimizations enabled, outputs 000
. This implies that
- the pointers are not equal, and
- neither of the pointers is ordered before the other according to
std::less
, implying that the two pointers are equal according to the total order imposed bystd::less
.
Is this behavior correct? Is the total order of std::less
not required to be consistend with equality operator?
Is the following program allowed to output 1
?
#include <iostream>
#include <set>
int main()
{
int i=0, j=0;
std::set<int *> s;
s.insert(&i);
s.insert(&j);
std::cout << s.size() << std::endl;
}