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: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.
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: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.