Boost C++ Libraries: Ticket #7652: compile-time checked access https://svn.boost.org/trac10/ticket/7652 <p> Since code like </p> <p> boost::array&lt;int,2&gt; test; test<a class="changeset" href="https://svn.boost.org/trac10/changeset/2" title="Add Boost Disclaimer">[2]</a> = 1; test[-1] = 1; </p> <p> compiles correctly on some compilers (even without warnings). I suggest adding compile checked functions for static access to arrays like: </p> <blockquote> <p> template&lt;size_type i&gt; reference at() { </p> <blockquote> <p> BOOST_STATIC_ASSERT( (i &lt; N) ); return elems[i]; </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> template&lt;size_type i&gt; const_reference at() const { </p> <blockquote> <p> BOOST_STATIC_ASSERT( (i &lt; N) ); return elems[i]; </p> </blockquote> <p> } </p> </blockquote> <p> then code like: </p> <p> boost::array&lt;int,2&gt; test; test.at&lt;2&gt; = 1; test.at&lt;-1&gt; = 1; </p> <p> would result in the expected errors. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7652 Trac 1.4.3 Marshall Clow Tue, 18 Dec 2012 16:24:46 GMT <link>https://svn.boost.org/trac10/ticket/7652#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7652#comment:1</guid> <description> <p> Yes, it would - but would people actually use it? </p> <p> Do people use: </p> <pre class="wiki">std::array&lt;int, 2&gt; arr; std::get&lt;2&gt;(arr) = -1; </pre><p> in C++11 - or do they just write <code>arr[2] = -1;</code> ? </p> <p> <code>boost::array</code> (and <code>std::array</code>, and <code>std::vector</code>) all have runtime checking through the member function <code>at</code>. </p> <pre class="wiki">boost::array&lt;int, 2&gt; arr; arr.at(2) = -1; // will throw an exception </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Tue, 18 Dec 2012 18:01:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7652#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7652#comment:2</guid> <description> <p> In <a class="changeset" href="https://svn.boost.org/trac10/changeset/82083" title="Add support for std::get&lt;&gt; to Boost.Array">[82083]</a>, I added support for <code>std::get&lt;&gt;</code> to Boost.Array, so you can write </p> <pre class="wiki">boost::array&lt;int, 2&gt; arr; std::get&lt;2&gt;(arr) = -1; </pre><p> and get a compile-time error. </p> <p> Right now, it's only turned on for C++11, but I might be able to make it work for C++03 systems. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 19 Dec 2012 06:26:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7652#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7652#comment:3</guid> <description> <p> The stl-version of VS 2008 has the same code for "get" and std::tr1::array&lt;T,N&gt; and it compiles fine with C++03, so it works at least for that compiler. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Tobias Loew</dc:creator> <pubDate>Wed, 19 Dec 2012 07:39:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7652#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7652#comment:4</guid> <description> <p> I made the last comment but forgot to login, sorry </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Thu, 27 Dec 2012 21:57:06 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7652#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7652#comment:5</guid> <description> <p> I also added overloads of <code>boost::get</code> for use on C++03 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Thu, 03 Jan 2013 17:27:41 GMT</pubDate> <title>status, milestone changed https://svn.boost.org/trac10/ticket/7652#comment:6 https://svn.boost.org/trac10/ticket/7652#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.54.0</span> </li> </ul> Ticket Marshall Clow Tue, 12 Feb 2013 18:11:23 GMT <link>https://svn.boost.org/trac10/ticket/7652#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7652#comment:7</guid> <description> <p> Also, in <a class="changeset" href="https://svn.boost.org/trac10/changeset/82834" title="Add constexpr support to Boost.Array">[82834]</a>, I marked a bunch of methods of Boost.Array as "constexpr". </p> </description> <category>Ticket</category> </item> </channel> </rss>