I have the following code:
std::set<std::pair<int, int>> my_set;
long long x = (some value);
long long y = (some value);
// should be true if x or y exceed the limits of int
bool z = my_set.find({x, y}) == my_set.end();
It works without any errors and seems to work correctly. But, I wonder if it's 'safe' because I am passing a pair of long long to the find function.
Thanks!
It is not safe in as far as the values that will actually be compared may not be what you expect.
The two
long longvalues will be converted tointand thenfindwill look for apaircontaining these two convertedintvalues. Since C++20 it is guaranteed that this conversion produces the unique value represented byintthat is equal to the original value modulo 2 to the power of the width ofint.If the values of
xandywill fall outside the range ofint, this means thatzmay becomefalseeven though there may not be a pair in the set for which the values ofxandyare (mathematically) equal to those of the pair in the set.However, aside from that, there is nothing wrong. The search after conversion from
long longtointwill work exactly as expected.