{"id":1685,"date":"2009-08-04T15:58:06","date_gmt":"2009-08-04T05:58:06","guid":{"rendered":"http:\/\/www.flamingspork.com\/blog\/?p=1685"},"modified":"2009-08-04T15:58:06","modified_gmt":"2009-08-04T05:58:06","slug":"stringstream-is-completely-useless-and-why-c-should-have-a-snprintf","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2009\/08\/04\/stringstream-is-completely-useless-and-why-c-should-have-a-snprintf\/","title":{"rendered":"stringstream is completely useless (and why C++ should have a snprintf)"},"content":{"rendered":"<ol>\n<li>It&#8217;s easy to screw up thread safety.<br \/>\nIf you&#8217;re trying to format something for output (e.g. leading zeros, only 1 decimal place or whatever&#8230; you know, format specifiers in printf) you are setting a property on the <strong>stream<\/strong>, not on what you&#8217;re converting. So if you have a thread running that sets a format, adds something to the stream, and then unsets the format, you <strong>cannot<\/strong> have another thread able to come in and do something to that stream. Look out for thread unsafe <strong>cout<\/strong> code.<\/li>\n<li>You cannot use streams for any text that may need to be translated.<br \/>\n<a href=\"http:\/\/www.gnu.org\/software\/gettext\">gettext<\/a> is what everybody uses. You cannot get a page into the manual before it tells you that translators may want to change the order of what you&#8217;re printing. This goes directly against stringstream.<\/li>\n<li>You need another reason? Number 2 rules it out for so much handling it&#8217;s not funny.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easy to screw up thread safety. If you&#8217;re trying to format something for output (e.g. leading zeros, only 1 decimal place or whatever&#8230; you know, format specifiers in printf) you are setting a property on the stream, not on &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2009\/08\/04\/stringstream-is-completely-useless-and-why-c-should-have-a-snprintf\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[76,75],"tags":[138,633,70,172,173],"class_list":["post-1685","post","type-post","status-publish","format-standard","hentry","category-code","category-drizzle-work-et-al","tag-c","tag-code","tag-drizzle","tag-stl","tag-stringstream"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-rb","jetpack-related-posts":[{"id":2182,"url":"https:\/\/www.flamingspork.com\/blog\/2010\/10\/26\/cursor-states\/","url_meta":{"origin":1685,"position":0},"title":"Cursor states","author":"Stewart Smith","date":"2010-10-26","format":false,"excerpt":"Following on from my post yesterday on the various states of a Storage Engine, I said I'd have a go with the Cursor object too. A Cursor is used by the Drizzle kernel to get and set data in a table. There can be more than one cursor open at\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.flamingspork.com\/blog\/wp-content\/uploads\/2010\/10\/graph1-284x300.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3745,"url":"https:\/\/www.flamingspork.com\/blog\/2014\/05\/26\/efficiently-writing-to-a-log-file-from-multiple-threads\/","url_meta":{"origin":1685,"position":1},"title":"Efficiently writing to a log file from multiple threads","author":"Stewart Smith","date":"2014-05-26","format":false,"excerpt":"There's a pattern I keep seeing in threaded programs (or indeed multiple processes) writing to a common log file. This is more of an antipattern than a pattern, and is often found in code that has existed for years. Basically, it's having a mutex to control concurrent writing to the\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2196,"url":"https:\/\/www.flamingspork.com\/blog\/2010\/11\/15\/limiting-functions-to-32k-stack-in-drizzle-and-scoped_ptr\/","url_meta":{"origin":1685,"position":2},"title":"Limiting functions to 32k stack in Drizzle (and scoped_ptr)","author":"Stewart Smith","date":"2010-11-15","format":false,"excerpt":"I wonder if this comes under \"Code Style\" or not... Anyway, Monty and I finished getting Drizzle ready for adding \"\u00ef\u00bb\u00bf\u00ef\u00bb\u00bf\u00ef\u00bb\u00bf-Wframe-larger-than=32768\" as a standard compiler flag. This means that no function within the Drizzle source tree can use greater than 32kb stack - it's a compiler warning - and with\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1395,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/02\/12\/performance-schema-show-me-the-code\/","url_meta":{"origin":1685,"position":3},"title":"Performance Schema: Show me the code","author":"Stewart Smith","date":"2009-02-12","format":false,"excerpt":"For such a long worked on feature, with such potential - I find the resistence to publishing a source tree curious (my comments on the topic have been moderated away but others have asked too). I could go and grep through the commits list searching for things (hint: look for\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2303,"url":"https:\/\/www.flamingspork.com\/blog\/2011\/03\/16\/fixed-in-drizzle-no-more-gotchas\/","url_meta":{"origin":1685,"position":4},"title":"Fixed in Drizzle: No more &#8220;GOTCHA&#8217;s&#8221;","author":"Stewart Smith","date":"2011-03-16","format":false,"excerpt":"\u00a0 At the upcoming MySQL Conference and Expo, I'm going to give a Thursday afternoon (2pm) session entitled Fixed in Drizzle: No more \"GOTCHA's\". I plan to have a lot of fun with this session.. If you go back to the very start of when I started submitting code to\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1738,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/10\/14\/return-of-the-top-5-mysql-wishlist-and-looking-at-drizzle\/","url_meta":{"origin":1685,"position":5},"title":"Return of the &#8220;Top 5 MySQL Wishlist&#8221; and looking at Drizzle","author":"Stewart Smith","date":"2009-10-14","format":false,"excerpt":"It's coming up on a year since I started working full time on Drizzle. So, I got a bit reflective... Have we done things that I (and others) really wanted done? Back in 2007, I wrote my top 5 wishlist for the MySQL Server. I am not going to pretend\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/1685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/comments?post=1685"}],"version-history":[{"count":2,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/1685\/revisions"}],"predecessor-version":[{"id":1687,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/1685\/revisions\/1687"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=1685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=1685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=1685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}