Operator ambiguity during the Studio 11 C++ compiler
Hi,
We are on the verge of upgrading the compiler from WS6 to 11. As such a need to compiler all the existing code which had been compiled successfully by WS6. However, we are facing some problem for the Template <class T> issue:
#include <sstream>
#include <string>
class ABC: public string
{public:
-- declaration of the data type
ABC(const char* str); ...
template<class T> ABC & operator+=(const T& var) {//100%
std::ostringstream strout;
strout.precision(40);
strout << var;
append(strout.str());
return *this;
}
ABC& operator+=(const string& str)
{
append( str );
return *this;
}
ABC& operator+=(const char* charstr)
{
append( charstr );
return *this;
}
ABC& operator+=(char ch)
{
((string*)this)->operator+=( ch );
return *this;
}
********** PROBLEM started **************
template<class T> friend ABC operator+(const string& s, const T& var)
{
ABC a(s);
return a+=var;
}
template<class T> friend ABC operator+(const T& var, const string& s)
{
ABC a(var);
return a+=s;
}
********** PROBLEM **************
Compilation error:
Error: Overloading ambiguity between "operator+<ABC>(const std::string &, const ABC &)" and "operator+(const ABC&, const char*)".
Can anyone shed some light on that? Any problem for the template declartion?
Many thanks in advance.
[1642 byte] By [
opsia] at [2007-11-26 15:13:38]

# 4
The code that we have:
#include <sstream>
#include <string>
class ABC: public string
{public:
-- declaration of the data type
ABC(const char* str); ...
template<class T> ABC & operator=(const T& var) {
std::ostringstream strout;
strout.precision(40);
strout << var;
assign(strout.str());
return *this;
}
ABC& operator=(const string& s) {
assign(s);
return *this;
}
ABC& operator=(const char* s) {
assign(s);
return *this;
}
template<class T> ABC & operator+=(const T& var) {
std::ostringstream strout;
strout.precision(40);
strout << var;
append(strout.str());
return *this;
}
ABC& operator+=(const string& str)
{
append( str );
return *this;
}
ABC& operator+=(const char* charstr)
{
append( charstr );
return *this;
}
ABC& operator+=(char ch)
{
((string*)this)->operator+=( ch );
return *this;
}
********** PROBLEM started **************
template<class T> friend ABC operator+(const string& s, const T& var)
{
ABC a(s);
return a+=var;
}
template<class T> friend ABC operator+(const T& var, const string& s)
{
ABC a(var);
return a+=s;
}
********** PROBLEM **************
-
};
# 5
There are three operator.
The first one: std::operator+<char, std::char_traits><char>, std::allocator<char>>(const std::string &, const char*)
The second one: template<class T> friend ABC operator+(const T& var, const string& s).
The third one: template<class T> friend ABC operator+(const string& s, const T& var).
If you write something like that: ABC a(""); a + ""; compiler tries to select the best match. But all operators require type cast to be called. The first one requires type cast for the first argument. Compiler has to cast ABC to string. The second one requires type cast for the second argument. Compiler has to cast const char * to const string. The third one requires type cast for the first argument. Compiler has to cast ABC to const string.
So compiler cannot select the best match and shows ambiguity error.
You can consider to rewrite friend template operators like that:
template<class T> friend ABC operator+(const T& var, const ABC& s).
template<class T> friend ABC operator+(const ABC& s, const T& var).
That should help ... but it does not help because Sun C++ compiler has a bug. Could you file a bug at http://bugs.sun.com?