Type conversion issue

B

balor

I've got a problem involving casting operators using GCC 3.2 -
hopefully someone here can help me out. I've written the following
small program:

class A {
public:
operator int() { return 2; }
};

int main() {
A a, b;
if (a == b);
}

This program compiles fine. But consider this slight permutation:

int main() {
std::vector<A> a, b;
if (a == b);
}

This version fails. If I can compare 2 A's in the 1st case, why can I
not in a vector? GCC reports that there is no match for the comparison
operator but then lists the internal operator(int, int) as a candidate.
If it found that as the candidate, then why not use it?

/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_algobase.h:
In
function `bool std::equal(_InputIter1, _InputIter1, _InputIter2)
[with
_InputIter1 = __gnu_cxx::__normal_iterator<const A*, std::vector<A,
std::allocator<A> > >, _InputIter2 =
__gnu_cxx::__normal_iterator<const A*,
std::vector<A, std::allocator<A> > >]':
/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_vector.h:765:
instantiated from `bool std::eek:perator==(const std::vector<_Tp,
_Alloc>&, const std::vector<_Tp, _Alloc>&) [with _Tp = A, _Alloc =
std::allocator<A>]'
a.cpp:10: instantiated from here
/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_algobase.h:681:
no
match for `const A& == const A&' operator
/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_algobase.h:681:
candidates
are: operator==(int, int) <built-in>

Thanks for the help.
 
V

Victor Bazarov

balor said:
I've got a problem involving casting operators using GCC 3.2 -
hopefully someone here can help me out. I've written the following
small program:

class A {
public:
operator int() { return 2; }

Declare this member 'const' and you'll have it.

V
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top