Is it safe to call std::equal on potentially shorter input if I know there will be difference detected within size of input
c++, stl
Solution
C++03 `std::equal()` requires the second sequence to be at least as long as the first one.
In C++14 there is another overload of `std::equal()` that takes two iterators for the second sequence.
You should convert the IP addresses into `uint32_t` and compare those instead, e.g.:
auto ip_prefix = ::inet_addr("111.222.233.0");
auto ip_mask = ::inet_addr("255.255.255.0");
bool compare(in_addr_t a, in_addr_t b, in_addr_t mask) {
return (a & mask) == (b & mask);
}
int main() {
std::cout << compare(ip_prefix, ::inet_addr("1.1.1.1"), ip_mask) << '\n';
std::cout << compare(ip_prefix, ::inet_addr("111.222.233.3"), ip_mask) << '\n';
}
Problem
While doing some nw programming I stumbled upon the following dilemma: Im doing something like: ``` static const string my_ip_prefix = "111.222.233"; //going through list of IPs where one might have prefix my_ip_prefix if (equal(my_ip_prefix .begin(), my_ip_prefix .end(), ip_list[i].begin()))) { // } ``` If I know IPs from `ip_list` can be shorter than my_ip_prefix, but that in that case they differ from `my_ip_prefix` on at least one position in them is it safe to call equal? Example : is it safe to call it with ip `"10.20.30.4"` Aka does standard mandates sequential checks starting from front and `break;` in `std::equal`? It might seem obvious that A is yes, but maybe ISO ppl wanted to give option implementations to parallelize...