Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Is it valid to have a comparator taking parameters of different type?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Juha Nieminen, post: 3809161"] Is it valid to use, for example, std::equal_range() with a comparator taking parameters of different type? It compiles and works correctly at least with gcc and MSVC++, but AFAIK at least in some versions of the latter, if you turn enough checks on, it will give an error because the parameter types are not the same. What does the standard say about this? Example code: //---------------------------------------------------------------- struct A { int value; A(int v=0): value(v) {} }; struct B { int value; B(int v=0): value(v) {} }; struct Comparator { bool operator()(const A& a, const B& b) const { return a.value < b.value; } bool operator()(const B& b, const A& a) const { return b.value < a.value; } }; #include <algorithm> #include <iostream> int main() { A table[1000]; for(int i = 0; i < 1000; ++i) table[i] = i/10; B element(25); std::pair<A*, A*> range = std::equal_range(table, table+1000, element, Comparator()); std::cout << "Range for value " << element.value << " starts at position " << range.first - table << " and is " << range.second - range.first << " elements long.\n"; } //----------------------------------------------------------------[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Is it valid to have a comparator taking parameters of different type?
Top