{"id":173,"date":"2014-11-12T07:35:00","date_gmt":"2014-11-12T15:35:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2014\/11\/12\/details-about-some-of-the-new-c-language-features\/"},"modified":"2019-07-31T09:25:43","modified_gmt":"2019-07-31T09:25:43","slug":"details-about-some-of-the-new-c-language-features","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/details-about-some-of-the-new-c-language-features\/","title":{"rendered":"Details About Some of the New C++ Language Features"},"content":{"rendered":"<p>When you read our <a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=517106\">release notes<\/a>, you&#8217;ll notice that we&#8217;ve added a large number of C++ language features. The details of what these features <em>are<\/em> can be found all over the web, but what <em>purpose<\/em> they serve might still be nebulous. This article will attempt to explain the origins of and how to use a few of these new language mechanisms for best results.<\/p>\n<h2>Terse Range-Based For Loops<\/h2>\n<p><em>[Note 11\/12\/2014 1:00pm PST: This proposed feature was approved by the Evolution and Core Working Groups in June 2014, and it was implemented in VS 2015 Preview and Clang 3.5, but the full Standardization Committee rejected this feature on Nov. 7, so it will be removed from VS 2015 RTM. New syntax will be proposed at the next Committee meeting in May 2015.]<\/em><\/p>\n<p>This proposed<em> <\/em>C++17 feature allows you to omit the type of the element in range-based for loops:<\/p>\n<p><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;std::<span style=\"color: #2b91af\">vector<span style=\"color: black\">&lt;<span style=\"color: blue\">int<span style=\"color: black\">&gt; v = { 1, 2, 3 }; <\/span><\/span><\/span><\/span><\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">for<span style=\"color: black\"> (i : v) <\/span><\/span><\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;{ <\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color: #a31515\">&#8220;%d &#8220;<span style=\"color: black\">, i); <\/span><\/span><\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/span><\/p>\n<p>You should keep in mind that this is more than a mere syntactic convenience.<\/p>\n<p>Many developers will tend to write <strong>auto<\/strong> as the element type, and in most cases this would work correctly, except that <strong>auto<\/strong> by itself causes the elements to be copied, which is usually not desirable. Furthermore, mutations to the element will not be reflected in the container.<\/p>\n<p>Some developers might try to get around these problems by writing <strong>auto &amp;<\/strong> as the element type instead. This will indeed work in a greater number of cases, but in some corner cases it also breaks:<\/p>\n<p><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;std::<span style=\"color: #2b91af\">vector<span style=\"color: black\">&lt;<span style=\"color: blue\">bool<span style=\"color: black\">&gt; v = { <span style=\"color: blue\">true<span style=\"color: black\">, <span style=\"color: blue\">false<span style=\"color: black\"> }; <\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">for<span style=\"color: black\"> (<span style=\"color: blue\">auto<span style=\"color: black\"> &amp;i : v)<span style=\"color: green\"> \/\/C4239<\/span><\/span><\/span><\/span><\/span><\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;{ <\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color: #a31515\">&#8220;%d &#8220;<span style=\"color: black\">, (<span style=\"color: blue\">bool<span style=\"color: black\">)i); <\/span><\/span><\/span><\/span><\/span><br \/> <span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/span><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/t0723a54.aspx\"><strong>std::vector&lt;bool&gt;<\/strong><\/a> type is specialized to be a bit vector, so a proxy type (a.k.a. &#8220;simulated reference&#8221;) is needed when iterating. The compiler will emit:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">warning C4239: nonstandard extension used: &#8216;initializing&#8217;: conversion from &#8216;std::_Vb_reference&lt;std::_Wrap_alloc&lt;std::allocator&lt;_Other&gt;&gt;&gt;&#8217; to &#8216;std::_Vb_reference&lt;std::_Wrap_alloc&lt;std::allocator&lt;_Other&gt;&gt;&gt; &amp;&#8217; <\/span><br \/><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp; &nbsp; with <\/span><br \/><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp; &nbsp; [ <\/span><br \/><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp; &nbsp; &nbsp; &nbsp; _Other=std::_Vbase <\/span><br \/><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp; &nbsp; ]<\/span><\/p>\n<p>If that extension were disabled, this code would result in a compilation error.<\/p>\n<p>For these reasons, a terse range-based for loop behaves the same as if you had written <strong>auto &amp;&amp; <\/strong>as the type of the element. This ensures that modifying the elements of the container will work right in cases where such modifications make sense, and that proxy types are handled correctly too.<\/p>\n<h2>Generalized Lambda Capture<\/h2>\n<p>To understand the reason for introducing this feature, also known as &#8220;init-capture&#8221;, imagine that you wish to generate some heap-allocated data to be operated on later with a lambda function:<\/p>\n<p><span style=\"color: blue;font-family: Consolas;font-size: 9pt;background-color: white\">auto<span style=\"color: black\"> f(<span style=\"color: blue\">int<span style=\"color: black\"> <span style=\"color: gray\">i<span style=\"color: black\">) <\/span><\/span><\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">{ <\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;std::<span style=\"color: #2b91af\">unique_ptr<span style=\"color: black\">&lt;<span style=\"color: blue\">int<span style=\"color: black\">&gt; p = std::make_unique&lt;<span style=\"color: blue\">int<span style=\"color: black\">&gt;(<span style=\"color: gray\">i<span style=\"color: black\">); <\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">return<span style=\"color: black\"> [=](<span style=\"color: blue\">int<span style=\"color: black\"> <span style=\"color: gray\">j<span style=\"color: black\">){ <span style=\"color: blue\">return<span style=\"color: black\"> *p + <span style=\"color: gray\">j<span style=\"color: black\">; }; <span style=\"color: green\">\/\/C2280<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">} <\/span><br \/>&nbsp;<br \/><span style=\"color: blue;font-family: Consolas;font-size: 9pt;background-color: white\">int<span style=\"color: black\"> main() <\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">{ <\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">auto<span style=\"color: black\"> g = f(42); <\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color: #a31515\">&#8220;%dn&#8221;<span style=\"color: black\">, g(2)); <\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color: #a31515\">&#8220;%dn&#8221;<span style=\"color: black\">, g(12)); <\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">return<span style=\"color: black\"> 0; <\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">}<\/span><\/p>\n<p>Unfortunately, this will not work. The compiler will emit:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">error C2280: &#8216;std::unique_ptr&lt;int,std::default_delete&lt;_Ty&gt;&gt;::unique_ptr(const std::unique_ptr&lt;_Ty,std::default_delete&lt;_Ty&gt;&gt; &amp;)&#8217;: attempting to reference a deleted function<\/span><\/p>\n<p>Because unique_ptr is a move-only type, capturing it from the enclosing scope by value is disallowed because it would require a copy.<\/p>\n<p>You could try to get around this via capturing by reference instead, replacing the return statement of <strong>f<\/strong> with:<\/p>\n<p><span style=\"color: black\"><span style=\"font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">return<span style=\"color: black\"> [&amp;](<span style=\"color: blue\">int<span style=\"color: black\"> <span style=\"color: gray\">j<span style=\"color: black\">){ <span style=\"color: blue\">return<span style=\"color: black\"> <span style=\"color: teal\">*<span style=\"color: black\">p + <span style=\"color: gray\">j<span style=\"color: black\">; };<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span> <\/span><\/p>\n<p>But <strong>p<\/strong> goes out of scope once <strong>f<\/strong> exits, so when the program is run, the output is gibberish, reflecting whatever garbage was on the stack at the time:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">1160179346 <\/span><br \/><span style=\"font-family: Consolas;font-size: 9pt\">681009 <\/span><\/p>\n<p>To truly solve this problem, you need a way to capture an instance of a move-only type by value. To do so, you can use generalized lambda capture, replacing the return statement of <strong>f<\/strong> with:<\/p>\n<p><span style=\"color: black\"><span style=\"font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">return<span style=\"color: black\"> [<em>q = std::move(p)<\/em>](<span style=\"color: blue\">int<span style=\"color: black\"> <span style=\"color: gray\">j<span style=\"color: black\">){ <span style=\"color: blue\">return<span style=\"color: black\"> *q + <span style=\"color: gray\">j<span style=\"color: black\">; };<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span> <\/span><\/p>\n<p>The italicized code <em>moves<\/em> the value of <strong>p<\/strong> into <strong>q<\/strong>, which is stored directly in the lambda function itself and thus will not expire until the lambda itself goes out of scope (in this case, at the end of <strong>main<\/strong>). The program now runs as expected and prints:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">44 <\/span><br \/><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">54 <\/span><\/p>\n<h2>Dining Philosophers<\/h2>\n<p>The &#8220;Dining Philosophers Problem&#8221; is a common scenario used to illustrate deadlock in concurrent resource allocation. For more details, read <a href=\"http:\/\/en.wikipedia.org\/wiki\/Dining_philosophers_problem\">this<\/a> Wikipedia article or one of the many other descriptions found on the internet or in a textbook.<\/p>\n<h3>Initial implementation<\/h3>\n<p>This uses the resource hierarchy solution to avoid deadlock.<\/p>\n<div class=\"WordSection1\">\n<p class=\"MsoNormal\" style=\"margin-bottom: .0001pt;line-height: normal\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">#include&nbsp;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&lt;<span class=\"SpellE\">cstdio<\/span>&gt;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">#include&nbsp;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&lt;thread&gt;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">#include&nbsp;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&lt;<span class=\"SpellE\">mutex<\/span>&gt;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">#include&nbsp;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&lt;vector&gt;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">class&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Fork<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span><br \/><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">private<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">:<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const&nbsp;<\/span><\/span><\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> position;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><span class=\"SpellE\"><span class=\"GramE\">std<\/span><\/span><span class=\"GramE\">::<span class=\"SpellE\"><span style=\"color: #2b91af\">mutex<\/span><\/span><\/span> m;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">public<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">:<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><span class=\"GramE\">Fork(<\/span><\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">) : position(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">) {}<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">bool<\/span><\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> pickup(<\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">) {<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">if<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> (<span class=\"SpellE\">m.try_lock<\/span>()) {<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span class=\"SpellE\"><span class=\"GramE\">printf<\/span><\/span><span class=\"GramE\">(<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&#8220;Philosopher %d picked up fork %d.n&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">this<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">-&gt;position);<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/span><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">return&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">true<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">return&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">false<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span>}<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">void<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> drop(<\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">) {<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span class=\"SpellE\"><span class=\"GramE\">m.unlock<\/span><\/span><span class=\"GramE\">(<\/span>);<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;<\/span><span class=\"SpellE\"><span class=\"GramE\">printf<\/span><\/span><span class=\"GramE\">(<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&#8220;Philosopher %d dropped fork %d.n&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">this<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">-&gt;position);<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span>}<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">};<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">class&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Philosopher<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span><br \/><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">private<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">:<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const&nbsp;<\/span><\/span><\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> position;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const<\/span><\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> <span class=\"SpellE\">std<\/span>::<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">vector<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Fork<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> *&gt; *forks;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">public<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">:<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><span class=\"GramE\">Philosopher(<\/span><\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int&nbsp;<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, <span class=\"SpellE\">std<\/span>::<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">vector<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Fork<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> *&gt; *<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">forks<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">) : position(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">position<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">), forks(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">forks<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">) {}<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">void<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> think() { <span class=\"SpellE\">printf<\/span>(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&#8220;Philosopher %d is thinking.n&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, position); _sleep(10); }<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">void<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> eat() { <span class=\"SpellE\">printf<\/span>(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&#8220;Philosopher %d is eating.n&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, position); _sleep(10); }<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">void<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> start() {<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: green;background: white\">\/\/ assume that the number of forks matches the number of philosophers<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int<\/span><\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> left = position;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int<\/span><\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"> right = (position + 1) % forks-&gt;size();<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;<\/span><br \/><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\"><span>&amp;<\/span><\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>When you read our release notes, you&#8217;ll notice that we&#8217;ve added a large number of C++ language features. The details of what these features are can be found all over the web, but what purpose they serve might still be nebulous. This article will attempt to explain the origins of and how to use a [&hellip;]<\/p>\n","protected":false},"author":6384,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[230],"tags":[8],"class_list":["post-173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-new-feature","tag-announcement"],"acf":[],"blog_post_summary":"<p>When you read our release notes, you&#8217;ll notice that we&#8217;ve added a large number of C++ language features. The details of what these features are can be found all over the web, but what purpose they serve might still be nebulous. This article will attempt to explain the origins of and how to use a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/6384"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}