我认为
显然,这并不起作用,因为to_string没有模板化。
to_string
只是一个模板,并在内部使用stringstream
。这不是这种情况吗?我想要能够做到这一点:class foo{};
ostream& operator<<(ostream& os, const foo& /*bar*/){
os << "foo";
return os;
}
int main() {
foo bar;
string tsTest = to_string(bar);
return 0;
}
显然,这并不起作用,因为to_string没有模板化。
std::stringstream
不是最有效的转换方法,可能这就是为令其无法成为模板化的原因,因此每个重载使用了更有效的函数? - Galikstd::to_string
是一组标准重载函数,而不是函数模板。 - Lightness Races in Orbitstd::to_string
是基于sprintf
而非std::ostringstream
定义的。很可能,其意图是可以这样实现。 - James Kanzeenable_if
来确定正在使用哪些类型,然后每种转换都有自己的处理逻辑。总体上应用的哲学似乎是任何可能失败的转换要么不编译,要么生成运行时异常。Facebook的大多数后端服务实际上都是用C++编写的。 - Mark