Re: Visual C++ Express won't compare object against float in std::upper_bound
"Ulrich Eckhardt" <eckhardt@satorlaser.com> wrote in message
news:hjata4-qnv.ln1@satorlaser.homedns.org
tron.thomas@verizon.net wrote:
Code like the following compiles and runs successfully on version 4.x
of the GNU compiler for Mac OS X (PPC) and Fedora Core Linux (Intel)
as well as Borland Turbo C++ on Windows XP:
Code like that or code identical to that? Provide a minimal example
please...
class Sample
{
public:
[...]
bool operator >
(
float position
) const
{
return m_position > position;
}
operator>
LHS=Sample
RHS=float
[...]
};
bool operator <
(
float position,
const Sample& sample
)
{
return sample > position;
}
operator<
LHS=float
RHS=Sample
SampleIterator midSample = std::upper_bound(samplings.begin(),
samplings.end(), 0.5f);
Uses operator<
I noticed that too. However, the issue is not whether the member and
non-member operators have the same order, but what order the upper_bound
function requires.
error C2678:
binary '<' : no operator found which takes a left-hand operand of
type '`anonymous-namespace'::Sample' (or there is no acceptable
conversion)
Yep, noticed that too, and drew the same conclusion as you did: that the
upper_bound function requires Sample as the LHS. Turns out, however, that:
1. Neither order compiles in Debug mode.
2. The *original* order compiles in Release mode.
Comeau online won't compile either order.
One way around the problem in Debug mode is to define the comparison
operator to take two Sample arguments rather than a Sample and a float.
Naturally, that also means supplying a Sample object as the third argument
to upper_bound.
--
John Carson