是否有一个警告自由模板函数将基本类型转换为字符串 [英] Is there a warning free template function to convert basic types to string
问题描述
我想提供一个模板函数,将大多数基本类型转换为字符串。
我到目前为止最好的是以下:
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();
}
用于以下操作:
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 ++说这些是不明确的,即使第一个的最坏转换比第二个的最坏转换更好
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();
}
但是这些有其他优点,
所以我想保留他们。如果我把第二个模板变成一个简单的方法,例如。 ostream的歧义被清除,但我在寻找sth。允许保留两个模板。
是否有一个通用函数提供相同的简单性,而不使用描述的选项生成警告?
如果没有,那么本地禁用已发出的警告的最好方法是什么?
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 ?
推荐答案
看起来你会从这样的场景得到这样的消息:
It seems you'd get such a message from a scenario like this:
#include <sstream>
#include <string>
#include <iostream>
struct Y {};
struct X
{
operator Y() const {return Y(); }
};
std::ostream& operator<< (std::ostream& os, X) { return os << "X"; }
std::ostream& operator<< (std::ostringstream& os, Y) { return os << "Y"; }
template<typename T> inline std::string anyToString(const T& var) {
std::ostringstream o;
o << var;
return o.str();
}
int main()
{
std::cout << anyToString(X()) << '\n';
}
我建议使用 code> flag。 GCC编译它在所有感谢编译器扩展,与其他编译器这将是一个直接的错误。
I'd recommend using the -pedantic
flag instead. GCC compiles it at all thanks to a compiler extension, with other compilers this will be a straight error.
添加:
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.
这实际上不适用于几种流类型。例如,如果 T
是 stringstream
,则 out<可能会返回一个
ostream
的引用,不能被隐式转换回 stringstream / code>参考。
This actually doesn't work for several stream types. E.g if T
is stringstream
, then out << angle.deg();
will likely return a reference to an ostream
which cannot be implicitly downcasted back to a stringstream
reference.
这篇关于是否有一个警告自由模板函数将基本类型转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!