{"id":2150,"date":"2023-02-26T19:03:45","date_gmt":"2023-02-26T18:03:45","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=2150"},"modified":"2023-02-26T19:03:47","modified_gmt":"2023-02-26T18:03:47","slug":"c23-multidimensional-subscript-operator","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2023\/02\/26\/c23-multidimensional-subscript-operator\/","title":{"rendered":"C++23: Multidimensional Subscript Operator"},"content":{"rendered":"\n<p>When you have a class representing multidimensional data, providing access to a specific element in this multidimensional space is often done by providing an <code>operator()<\/code> with as many parameters as there are dimensions. You had to resort to <code>operator()<\/code>, because <code>operator[]<\/code> only supported a single index.<\/p>\n\n\n\n<p>That&#8217;s now history, as C++23 introduces the <strong>multidimensional subscript operator<\/strong>. Providing such an operator for your class is straightforward:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [13]; title: ; notranslate\" title=\"\">\nimport std;\n\ntemplate &lt;typename T&gt;\nclass Matrix\n{\npublic:\n\tMatrix(std::size_t rows, std::size_t cols)\n\t\t: m_rows{ rows }, m_cols{ cols }\n\t{\n\t\tm_data.resize(rows * cols);\n\t}\n\n\tT&amp; operator&#x5B;](std::size_t x, std::size_t y) { return m_data&#x5B;x + y * m_cols]; }\n\nprivate:\n\tstd::size_t m_rows;\n\tstd::size_t m_cols;\n\tstd::vector&lt;T&gt; m_data;\n};\n<\/pre><\/div>\n\n\n<p>The class can be tested as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [9,17]; title: ; notranslate\" title=\"\">\nconst std::size_t Rows{4};\nconst std::size_t Cols{4};\nMatrix&lt;int&gt; m{ Rows, Cols };\nstd::size_t counter{ 0 };\nfor (std::size_t y{ 0 }; y &lt; Rows; ++y)\n{\n\tfor (std::size_t x{ 0 }; x &lt; Cols; ++x)\n\t{\n\t\tm&#x5B;x, y] = ++counter;\n\t}\n}\n\nfor (std::size_t y{ 0 }; y &lt; Rows; ++y)\n{\n\tfor (std::size_t x{ 0 }; x &lt; Cols; ++x)\n\t{\n\t\tstd::print(&quot;{} &quot;, m&#x5B;x, y]);\n\t}\n\tstd::println(&quot;&quot;);\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>When you have a class representing multidimensional data, providing access to a specific element in this multidimensional space is often done by providing an operator() with as many parameters as there are dimensions. You had to resort to operator(), because operator[] only supported a single index. That&#8217;s now history, as C++23 introduces the multidimensional subscript [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[248],"class_list":["post-2150","post","type-post","status-publish","format-standard","hentry","category-c","tag-c23"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/2150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2150"}],"version-history":[{"count":8,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/2150\/revisions"}],"predecessor-version":[{"id":2158,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/2150\/revisions\/2158"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}