【发布时间】:2015-06-02 20:53:21
【问题描述】:
即使在模板中我可以有任何类型,to_string 函数也不适用于基本字符串:
例如:
std::string str("my string");
my_class(str);
使用这个函子定义:
template<class valuetype>
void operator()(valuetype value)
{
...
private_string_field = std::to_string(value);
不起作用。这是错误:
错误:没有匹配的调用函数 ‘to_string(std::basic_string&)’
避免它的最佳方法是什么。
提前,我请求避免仅仅因为一些常见的关键字而链接到不相关的问题。
【问题讨论】:
-
我不打算讨论不为
to_string重载std::string是否有意义,但是......为什么在上面的代码中使用它?operator<<可以处理以较低成本定义to_string的任何内容(无需通过中间std::string) -
@DavidRodríguez-dribeas 你是对的。我只是想缩短代码,所以我放了一个
cout。事实上,我需要将它存储在某个地方并打印以备后用。在日志文件中。
标签: c++ string c++11 compiler-errors