conversion operator ambiguity
Could someone please throw some light or suggest a fix to the
ambiguous overload errors that gcc is complaining about? It only seems
to happen in the presence of multiple conversion operators. If if
remove lines A and B, then it compiles.
#include <string>
struct C {
std::string s; int i;
operator std::string() const { return s; }
operator int() const { return i; } // line A
};
int main()
{
C c;
std::string s;
int i;
s = c;
i = c; // line B
}
$ g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
$ g++ -o tp tp.cpp
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/
bits/basic_string.h:485: note: candidates are:
std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT,
_Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits,
_Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>,
_Alloc = std::allocator<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/
bits/basic_string.h:493: note:
std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT,
_Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char,
_Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near
match>
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/
bits/basic_string.h:504: note:
std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT,
_Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]