我想提供一个将大多数基本类型转换为字符串的模板化函数。到目前为止,我想出的最好的方法如下:
template<typename T> inline std::string anyToString(const T& var) {
std::ostringstream o;
o << var;
return o.str();
}
例如,该功能可用于以下用途:
class TimeError:public std::runtime_error{
public:
explicit TimeError(int time):std::runtime_error(anyToString(time)),
mTime(time){};
protected:
int mTime;
};
anyToString 和类似函数的问题是使用 gcc 版本 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“
据我所知,警告的原因在于调用 << 时的隐式转换可能性。
这些歧义主要是由其他模板产生的,如下所示:
template<typename T>
T& operator<<(T& out, const SymRad& angle){
return out << angle.deg();
}
但这些还有其他优点,例如适用于多种流类型。所以我想保留它们。如果我将第二个模板转换为例如 ostream 的普通方法,则会清除歧义,但我正在寻找某事。这允许保留两个模板。是否有一个通用函数可以提供相同的简单性而不会使用所描述的选项生成警告?如果没有,在本地禁用发出的警告的最佳方法是什么?