Is passing a pair of long long in set find safe?

97 Views Asked by At

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!

2

There are 2 best solutions below

6
user17732522 On BEST ANSWER

It is not safe in as far as the values that will actually be compared may not be what you expect.

The two long long values will be converted to int and then find will look for a pair containing these two converted int values. Since C++20 it is guaranteed that this conversion produces the unique value represented by int that is equal to the original value modulo 2 to the power of the width of int.

If the values of x and y will fall outside the range of int, this means that z may become false even though there may not be a pair in the set for which the values of x and y are (mathematically) equal to those of the pair in the set.

However, aside from that, there is nothing wrong. The search after conversion from long long to int will work exactly as expected.

2
fabian On

You are not passing a pair of long long to the function. Since you did not specify the type of the key passed to std::set::find, the compiler uses the constructor taking a lvalue reference to a const Key object, i.e. in this case std::pair<int, int>.

This comes with all drawbacks of you creating a std::pair<int, int> from values that may exceed the range of int.

std::set<std::pair<int, int>> my_set;

my_set.emplace(0, 0);

long long x = 0x100000000;
long long y = 0;

// should be false if x or y exceed the limits of int
bool z = my_set.find({ x, y }) == my_set.end();
std::cout << std::boolalpha << z << '\n'; // prints false