Use of lower_bound

J

Jim West

The following code does what I want it to do on two different compilers
(g++ and Intel icc under Linux), but I'm not sure if it is undefined
and just happens to work:


class A {
public:
int i;
};

bool comp_A_i (const A &a, int i) {
return a.i < i;
}

int main( ) {
std::vector<A> v(6);
int i;
std::vector<A>::iterator pos
= lower_bound(v.begin(), v.end(), i, comp_A_i);
}

Is the function comp_A_i properly defined having two different
argument types?
 
H

Howard Hinnant

Jim West said:
The following code does what I want it to do on two different compilers
(g++ and Intel icc under Linux), but I'm not sure if it is undefined
and just happens to work:


class A {
public:
int i;
};

bool comp_A_i (const A &a, int i) {
return a.i < i;
}

int main( ) {
std::vector<A> v(6);
int i;
std::vector<A>::iterator pos
= lower_bound(v.begin(), v.end(), i, comp_A_i);
}

Is the function comp_A_i properly defined having two different
argument types?

Strictly speaking, this is not guaranteed to work in C++03. But I
wouldn't sweat it. Every major vendor has agreed that it should work,
and also agreed to revise the next version of the standard to enforce
that decision:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#270

So from a practical standpoint, you're good to go. And it will be made
official some time down the road.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top