I want to provide a templated function that converts most basic types to string. The best I have come up with so far is the following:
template<typename T> inline std::string anyToString(const T& var) {
std::ostringstream o;
o << var;
return o.str();
}
The function can e.g. be used for the following:
class TimeError:public std::runtime_error{
public:
explicit TimeError(int time):std::runtime_error(anyToString(time)),
mTime(time){};
protected:
int mTime;
};
The problem with anyToString and similar functions is the generation of ambiguity warnings when compiling with gcc version 4.4.3 -Wall -Wexta -Werror
"ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second
"
To my knowledge the reason for the warning lies in the implicit conversion possibilities when calling <<.
Those ambiguities are mainly generated by other templates as the following:
template<typename T>
T& operator<<(T& out, const SymRad& angle){
return out << angle.deg();
}
But those have other advantages like working for several stream types. So I would like to keep them. If I turn the second template into a plain method for e.g. ostream the ambiguity is cleaned, but I'm looking for sth. that allows keeping both templates. Is there a generic function that does provide the same simplicity without generating warnings using the described options ? If not, what is the best way to locally disable the issued warning ?