C
Christof Warlich
Hi,
is there any danger in overloading operator+ as follows:
template<typename T> string operator+(const string &x, T y) {
ostringstream tmp;
tmp << x << y;
return tmp.str();
}
apart from the fact that it does conflict with the definition from the
standard string library, which already provides dedicated overloads for:
string operator+ (const string& lhs, const string& rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (char lhs, const string& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (const string& lhs, char rhs);
As an (ugly) workaround, I overloaded operator&() instead, i.e.
template<typename T> string operator&(const string &x, T y) {
ostringstream tmp;
tmp << x << y;
return tmp.str();
}
but I wonder if there was a good reason that the standard only provides
the dedicated overloads for operator+() on strings instead of the more
generic one employing a template for the second parameter.
is there any danger in overloading operator+ as follows:
template<typename T> string operator+(const string &x, T y) {
ostringstream tmp;
tmp << x << y;
return tmp.str();
}
apart from the fact that it does conflict with the definition from the
standard string library, which already provides dedicated overloads for:
string operator+ (const string& lhs, const string& rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (char lhs, const string& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (const string& lhs, char rhs);
As an (ugly) workaround, I overloaded operator&() instead, i.e.
template<typename T> string operator&(const string &x, T y) {
ostringstream tmp;
tmp << x << y;
return tmp.str();
}
but I wonder if there was a good reason that the standard only provides
the dedicated overloads for operator+() on strings instead of the more
generic one employing a template for the second parameter.