{"id":715,"date":"2017-11-24T23:20:11","date_gmt":"2017-11-25T07:20:11","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=715"},"modified":"2017-11-28T04:42:41","modified_gmt":"2017-11-28T12:42:41","slug":"begin-and-end-is-defined-for-arrays-in-c11-and-above","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2017\/11\/24\/begin-and-end-is-defined-for-arrays-in-c11-and-above\/","title":{"rendered":"begin() and end() is defined for arrays in C++11 and above"},"content":{"rendered":"<p>I was a little confused the first time I saw range-based for-loop in C++11 on &#8220;Tour of C++&#8221; that it works right out of the box for both <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2017\/11\/25\/understanding-the-difference-between-recognized-arrays-and-pointers\/\"><em>recognized<\/em> array<\/a> and STL containers and yet the text says it requires begin() and end() to be defined for the object for range-based for-loop to run.<\/p>\n<p>I later learned that despite typical STL usage example writes <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">v.begin(), v.end()<\/code>, the most bulletproof way is to write <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">begin(v), end(v)<\/code> instead (Herb Sutter recommends it). Then I started to suspect that C++11 must have defined free-form (non-member) <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">begin(), end()<\/code> functions that takes in arbitrary <em>recognized<\/em> arrays. I pulled up my code editor and ran this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\nint main()\r\n{\r\n    int v[4]={1,2,3,4};\r\n    std::cout &lt;&lt; *(std::crend(v)-1) &lt;&lt; std::endl;\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>It compiled and ran uneventfully, printing &#8216;1&#8217; as expected (I&#8217;m using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">crend()<\/code>, to see if they implemented the more obscure ones). It makes more sense now why range-based for-loop works for arbitrary <em>recognized<\/em> arrays without making an exception to the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">begin(), end()<\/code> requirement.<\/p>\n<p>To confirm that it is the case (since &#8220;Tour of C++&#8221; didn&#8217;t say anything about why arbitrary array works for range-based for loop), I looked up the STL source code from libc++ in LLVM, namely &lt;<a href=\"https:\/\/llvm.org\/svn\/llvm-project\/libcxx\/trunk\/include\/iterator\">iterator<\/a>&gt;, and saw this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">template\u00a0&lt;class\u00a0T,\u00a0size_t\u00a0N&gt;\u00a0constexpr\u00a0T*\u00a0begin(T\u00a0(&amp;array)[N]);<\/pre>\n<p>Bingo! There&#8217;s a mechanism to do so. But before I close, Stephan T. Lavavej (Mr. STL) <a href=\"https:\/\/channel9.msdn.com\/Events\/GoingNative\/GoingNative-2012\/STL11-Magic-Secrets\">mentioned<\/a> that the template quoted above is no longer required (by the standard) to implement range-based for-loop in C++11.<\/p>\n<p>Now the conclusion becomes that\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">begin(), end()<\/code>\u00a0that takes in <em>recognized<\/em> arrays exist (which completes the logic behind range-based for-loop), but the range-based for loop can (and typically will) handle <em>recognized<\/em> arrays without these templated functions defined.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_715\" class=\"pvc_stats all  \" data-element-id=\"715\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I was a little confused the first time I saw range-based for-loop in C++11 on &#8220;Tour of C++&#8221; that it works right out of the box for both recognized array and STL containers and yet the text says it requires &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2017\/11\/24\/begin-and-end-is-defined-for-arrays-in-c11-and-above\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_715\" class=\"pvc_stats all  \" data-element-id=\"715\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[25,6,29],"tags":[],"class_list":["post-715","post","type-post","status-publish","format-standard","hentry","category-cpp","category-note-to-self","category-programming"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=715"}],"version-history":[{"count":17,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/715\/revisions"}],"predecessor-version":[{"id":770,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/715\/revisions\/770"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}