鸵鸟流和结束 [英] ostringstream and ends

查看:163
本文介绍了鸵鸟流和结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在和别人的代码一起工作,注意到在所有使用ostringsteam的时候,他们习惯于显式地添加 std :: ends

I've been working with somebody else's code and noticed that on all uses of ostringsteam they are in the habit of explicitly appending std::ends.

这是我从来没有做过的,从来没有遇到过问题。

This is something I've never done and have never encountered a problem.

它不会出现, std :: ends 在以下代码中有什么区别?

It doesn't appear to, but should std::ends make any difference in the following code?

ostringstream message;
message << "Hello world, version " << 2 /* << std::ends ??? */;
printf( "%s\n", message.str().c_str() );


推荐答案

附加 std :: ends 的 会返回一个以null结尾的 char * 。同样的不是(现已过时) strstream s的情况,其中追加 std :: ends 是必要的。我相信作者根本不知道这种改变的行为。

Appending std::ends is nonsense here since stringstream’s c_str returns a null-terminated char*. The same was not the case for the (now deprecated) strstreams where appending std::ends was necessary. I believe the author simply didn’t know of this changed behaviour.

这篇关于鸵鸟流和结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆