{"id":405,"date":"2015-02-17T17:01:10","date_gmt":"2015-02-17T22:01:10","guid":{"rendered":"http:\/\/blog.mozilla.org\/nfroyd\/?p=405"},"modified":"2015-02-17T17:01:10","modified_gmt":"2015-02-17T22:01:10","slug":"multiple-return-values-in-c","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nfroyd\/2015\/02\/17\/multiple-return-values-in-c\/","title":{"rendered":"multiple return values in C++"},"content":{"rendered":"<p>I&#8217;d like to think that I know a fair amount about C++, but I keep discovering new things on a weekly or daily basis.\u00a0 One of my recent sources of new information is <a href=\"https:\/\/github.com\/CppCon\/CppCon2014\/\">the presentations from CppCon 2014<\/a>.\u00a0 And the most recent presentation I&#8217;ve looked at is Herb Sutter&#8217;s <a href=\"https:\/\/github.com\/CppCon\/CppCon2014\/tree\/master\/Presentations\/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style\">Back to the Basics: Essentials of Modern C++ Style<\/a>.<\/p>\n<p>In the presentation, Herb mentions a feature of <code>tuple<\/code> that enables returning multiple values from a function.\u00a0 Of course, one can already return a <code>pair&lt;T1, T2&gt;<\/code> of values, but accessing the fields of a <code>pair<\/code> is suboptimal and not very readable:<\/p>\n<pre>pair&lt;...&gt; p = f(...);\r\nif (p.second) {\r\n  \/\/ do something with p.first\r\n}\r\n<\/pre>\n<p>The designers of <code>tuple<\/code> must have listened, because of the function <code>std::tie<\/code>, which lets you destructure a <code>tuple<\/code>:<\/p>\n<pre>typename container::iterator position;\r\nbool already_existed;\r\nstd::tie(position, already_existed) = mMap.insert(...);\r\n<\/pre>\n<p>It&#8217;s not quite as convenient as destructuring multiple values in other languages, since you need to declare the variables prior to <code>std::tie<\/code>&#8216;ing them, but at least you can assign them sensible names. And since <code>pair<\/code> implicitly converts to <code>tuple<\/code>, you can use <code>tie<\/code> with functions in the standard library that return <code>pair<\/code>s, like the insertion functions of associative containers.<\/p>\n<p>Sadly, we&#8217;re somewhat limited in our ability to use shiny new concepts from the standard library because of our C++ standard library situation on Android (we use stlport there, and it doesn&#8217;t feature useful things like <code>&lt;tuple&gt;<\/code>, <code>&lt;function&gt;<\/code>, or <code>&lt;thread_local&gt;<\/code>. We could, of course, polyfill some of these (and other) headers, and indeed we&#8217;ve done some of that in <a href=\"http:\/\/mxr.mozilla.org\/mozilla-central\/source\/mfbt\/\">MFBT<\/a> already. But using our own implementations limits our ability to share code with other projects, and it also takes up time to produce the polyfills and make them appropriately high quality. I&#8217;ve seen several people complain about this, and I think it&#8217;s something I&#8217;d like to fix in the next several months.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;d like to think that I know a fair amount about C++, but I keep discovering new things on a weekly or daily basis.\u00a0 One of my recent sources of new information is the presentations from CppCon 2014.\u00a0 And the most recent presentation I&#8217;ve looked at is Herb Sutter&#8217;s Back to the Basics: Essentials of [&hellip;]<\/p>\n","protected":false},"author":320,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[540],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/405"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/users\/320"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/comments?post=405"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/405\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/media?parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/categories?post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/tags?post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}