By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Type conversion issue

P: n/a
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::operator==(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.

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
balor wrote:
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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.