lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] Why have all relational operators for containers?


From: Greg Chicares
Subject: [lmi] Why have all relational operators for containers?
Date: Wed, 24 Mar 2021 12:20:41 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0

I was surprised that this is not an error:

#include <algorithm>
#include <vector>

int main(int, char*[])
{
    std::vector<int> v0 {4, 5, 6};
    std::vector<int> v1 {3, 9};
    std::vector<int> v2 = max(v0, v1); // Not an error.
    std::cout << v2.size() << std::endl;
    std::cout << v2[0] << ", " << v2[1] << ", " << v2[2] << std::endl;
    return 0;
}

I thought that would be an error. Instead, it prints "4, 5, 6",
because v0 lexicographically precedes v1.

For what reason would the standard provide containers with relational
operators other than '==' and '!='? So that we can sort a vector of
vectors? Is there a stronger rationale that I don't see?

How would the language be worse if it didn't define [pre-C++20]
  template<typename T, typename A>
  bool operator<(std::vector<T,A> const& lhs, std::vector<T,A> const& rhs);
with the following effects?
  std::max(v0, v1); // Error, hypothetically.
  std::max(v0, v1, compare); // Okay.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]