[英]How to use an ostringstream in just an expression
I use std::ostringstream
for formatting strings, which inherits its <<
operators from ostream
, and consequently they return an ostream
and not an ostringstream
, which means that I can't call ostringstream::str
on the result. 我使用
std::ostringstream
来格式化字符串,它继承了来自ostream
<<
运算符,因此它们返回一个ostream
而不是一个ostringstream
,这意味着我不能在结果上调用ostringstream::str
。 This usually isn't a problem, since I can typically do this: 这通常不是问题,因为我通常可以这样做:
ostringstream stream;
stream << whatever;
string str = stream.str();
But occasionally I need* to use it in just a single expression, which is more difficult. 但偶尔我需要*只在一个表达式中使用它,这更难。 I could do
我可以
string str = ((ostringstream&) (ostringstream() << whatever)).str();
but bad things will happen if that <<
overload returns some ostream
that isn't an ostringstream
. 但是如果
<<
overload返回一些不是ostringstream
ostream
,那么会发生坏事。 The only other thing I can think to do is something like 我能想到的另一件事就是
string str = ([&] () -> string {ostringstream stream; stream << whatever; return stream.str();})();
but this can't possibly be efficient, and is certainly very bad c++ code. 但这不可能有效,而且肯定是非常糟糕的c ++代码。
*Okay, I don't need to, but it would be a lot more convenient to. *好的,我不需要 ,但它会更方便。
Using 运用
string str = ([&] () -> string
{
ostringstream stream;
stream << whatever;
return stream.str();
})();
is ok. 没关系。 However, I think it will be better to name that operation, create a function with that name, and call the function.
但是,我认为命名该操作,创建具有该名称的函数并调用该函数会更好。
namespace MyApp
{
template <typename T>
std::string to_string(T const& t)
{
ostringstream stream;
stream << t;
return stream.str();
}
}
and then use 然后使用
string str = MyApp::to_string(whatever);
The only other thing I can think to do is something like
我能想到的另一件事就是
string str = ([&] () -> string {ostringstream stream; stream << whatever; return stream.str();})();
but this can't possibly be efficient, and is certainly very bad c++ code.
但这不可能有效,而且肯定是非常糟糕的c ++代码。
Actually, no. 实际上,没有。 Once the optimizer has a look at this, it's exactly as fast as the original code.
一旦优化器看到它,它就和原始代码一样快。 And it's not "very bad c++ code".
而且它不是“非常糟糕的c ++代码”。 It's known as an Immediately Invoked Lambda Expression, or IILE.
它被称为立即调用的Lambda表达式,或IILE。 It's an idiom, and actually quite decent practice.
这是一个成语,实际上是相当不错的练习。
It would be better formatted more like this, though: 不过,它会更好地格式化,但是:
// One of the benefits of IILEs is that `const` can be used more frequently
string const str = [&] {
ostringstream stream;
stream << whatever;
return stream.str();
}();
Some people prefer using std::invoke
to invoke the lambda instead: 有些人更喜欢使用
std::invoke
来调用lambda:
string const str = std::invoke([&] {
ostringstream stream;
stream << whatever;
return stream.str();
});
Assuming you got 假设你有
std::string get_str(std::ostream& out) {
retrun static_cast<std::stringbuf*>(out.rdbuf())->str();
}
You could use 你可以用
std:: string s = get_str(std::ostringstream{} << ...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.