J
Juha Nieminen
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/10;
B element(25);
std:air<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";
}
//----------------------------------------------------------------
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/10;
B element(25);
std:air<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";
}
//----------------------------------------------------------------