c ++重载运算符<<对于std :: string [英] c++ overloading operator << for std::string
问题描述
我现在看到了另一个使用sprintf将C内建函数附加到一个char数组中的天真的C ++代码,我想足够了.
I'm just now seeing another naive C++ code using sprintf to append C builtins into an array of chars, and I guess enough is enough.
我可以为std::string
提供简单,轻便,附加和非格式化的功能,但是由于它已被纳入团队的通用代码中,因此我希望它是完美的,因此我需要一些在此功能的界面上提供的建议(即不在实际实现上).
I could help providing with simple, lightweight, appending and non-formatting functions for std::string
, but as it would be check-in-ed into the team's common code, I want it to be perfect, so I need some advice on the interface of this feature (i.e. not on the actual implementation).
以下可能(我没有测试,这只是预感):
The following could be possible (I did not test it, it's just a hunch):
- 重载"
+=
"运算符(可能是在除std或global之外的其他命名空间中) - 重载"
<<
"运算符(同样,在另一个命名空间中) - 提供非操作员的非成员函数(我想还是在另一个名称空间中提供)
- 我没有看到的另一个简单解决方案?
- Overloading the "
+=
" operator (probably in another namespace than std or global) - Overloading the "
<<
" operator (again, in another namespace) - Providing non-operator non-member functions (I guess, again in another namespace)
- Another easy solution I did not see?
每种解决方案的利弊是什么(我偏爱"+=
",甚至是"<<
")?
What would be the pros and the cons of each solution (I have a preference for "+=
", or even "<<
") ?
- 重点不在于格式化.如果有人想要格式化,那么C ++流就可以了.我只想要简单,轻巧的一个语句/函数调用追加.
- 使用另一个命名空间是因为我们无权向std命名空间添加代码,并且我不想污染全局命名空间,所以,是的,我想用户必须添加
对 <utility>
的 rel_ops 命名空间)所做的工作 li>- 我正在使用
std::string
这本来无法处理的问题除了char
和char *
之外的其他类型.我想将其扩展为处理其他简单类型.- 在代码方面,使用stringstream的权重太大(声明流,追加,然后检索
.str()
并将其放入字符串等),而我想要的最后一件事是语法加糖的内联函数在每次调用时实例化一个字符串流).如下面的示例所示,stringstream解决方案是太冗长: - 我正在使用
- the point is not about formatting. If someone wants formatting, C++ streams are good for that. I just want simple, lightweight, one statement/function call appending.
- The use of another namespace would be because we are not authorized to add code to the std namespace, and I don't want to pollute the global namespace, so, yes, I guess the user would have to add a
using namespace SomeNamespace ;
as its done for the<utility>
's rel_ops namespace) - I'm using
std::string
which is not able, natively, to handle other types than itself,char
andchar *
. I want to extend that to handle other simple types. - Using a stringstream weights too much in term of code (declaring the stream, appending, then retrieving the
.str()
to put it inside a string, etc. etc.), and the last thing I want is an syntactic sugared inline function instanciating a stringstream at each call). As you can see in the example below, the stringstream solution is too verbose:
.
// sprintf-like code with a char[] buffer:
sprintf(buffer, "%d", myDouble) ;
// stream-like code with a std::string buffer:
std::stringstream str ;
str << myDouble ;
buffer = str.str() ;
// example of desired code with a std::string buffer:
buffer += myDouble ;
推荐答案
-
boost::format
怎么样?然后您可以编写:
What about
boost::format
? Then you can write:
std::string first("world");
std::string s = (boost::format("hello %1%") % first).str();
或创建一个包装类,您可以像这样使用它:
Or create a wrapper class which you can use like so:
int i(2);
std::string s = (Format() + "Hello " + first + " " + i).str();
和Format()
类似(没有增强)的东西:
class Format
{
public:
template <typename T>
Format &operator+(const T& v) {
m_sstr << v;
return *this;
};
const std::string &str() const { return m_sstr.str(); };
private:
std::stringstream m_sstr;
};
这篇关于c ++重载运算符<<对于std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!