Boost C++ Libraries: Ticket #2857: change return type of the c_array() member function https://svn.boost.org/trac10/ticket/2857 <p> Currently the return type of c_array() member function is T* for boost::array&lt;T,N&gt;. Regarding its name we could expect from this function to return a reference on a C array rather than a pointer on the first element of the array. </p> <p> In returning T* type a piece of information about the C array is lost since the static size N is not transmitted. </p> <p> I suggest to change the existing c_array() member function: </p> <pre class="wiki">T* c_array() { return elems; } </pre><p> into, </p> <pre class="wiki">T (&amp;c_array())[N] { return elems; } </pre><p> The both implementation are pretty similar since a reference on array is implicitly convertible into a pointer on the 1st element of this array. </p> <p> These following kind of lines of code would still work: </p> <pre class="wiki">array&lt;int,10&gt; toto; int* item_ptr = toto.c_array(); </pre><p> but purist would now be able to write </p> <pre class="wiki">int (&amp;toto_ref)[10] = toto.c_array(); </pre><p> The improvement which would be brought by the change is that in our example sizeof( toto.c_array() ) would return value 10*sizeof(int), the real size of the C array. </p> <p> best regards, herve </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2857 Trac 1.4.3 niels_dekker Sun, 15 Mar 2009 13:44:27 GMT <link>https://svn.boost.org/trac10/ticket/2857#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2857#comment:1</guid> <description> <p> Discussion on this subject: "[boost][array] change proposal for the c_array() function" <a class="ext-link" href="http://thread.gmane.org/gmane.comp.lib.boost.devel/187487"><span class="icon">​</span>http://thread.gmane.org/gmane.comp.lib.boost.devel/187487</a> <a class="ext-link" href="http://lists.boost.org/Archives/boost/2009/03/149711.php"><span class="icon">​</span>http://lists.boost.org/Archives/boost/2009/03/149711.php</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>niels_dekker</dc:creator> <pubDate>Wed, 18 Mar 2009 17:32:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2857#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2857#comment:2</guid> <description> <p> David Abrahams and I just mailed about the issue and we agreed that it would be preferable to have such a function as a non-member. Because tr1::array doesn't have a c_array() member function and std::array probably won't have one either. (See <a class="ext-link" href="http://home.roadrunner.com/~hinnant/issue_review/lwg-active.html#930"><span class="icon">​</span>LWG issue 930, Access to std::array data as built-in array type</a>) Such a boost::get_c_array(arg) function could be overloaded to support both std::array and boost::array. So it could be implemented as follows: </p> <pre class="wiki"> // Specific for boost::array: simply returns its elems data member. template &lt;typename T, std::size_t N&gt; T(&amp;get_c_array(boost::array&lt;T,N&gt;&amp; arg))[N] { return arg.elems; } // Const version. template &lt;typename T, std::size_t N&gt; const T(&amp;get_c_array(const boost::array&lt;T,N&gt;&amp; arg))[N] { return arg.elems; } // Overload for std::array, assuming that std::array will have // explicit conversion functions as discussed at the WG21 meeting // in Summit, March 2009. template &lt;typename T, std::size_t N&gt; T(&amp;get_c_array(std::array&lt;T,N&gt;&amp; arg))[N] { return static_cast&lt;T(&amp;)[N]&gt;(arg); } // Const version. template &lt;typename T, std::size_t N&gt; const T(&amp;get_c_array(const std::array&lt;T,N&gt;&amp; arg))[N] { return static_cast&lt;T(&amp;)[N]&gt;(arg); } </pre><p> When rvalue references are supported, get_c_array could be overloaded for rvalue arrays as well. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>niels_dekker</dc:creator> <pubDate>Thu, 06 Aug 2009 12:22:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2857#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2857#comment:3</guid> <description> <p> Update: <em>std::array</em> might still have <em>c_array()</em> member functions, as the proposed resolution of <a class="ext-link" href="http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#930"><span class="icon">​</span>LWG #930</a> was reconsidered by the Standard Library Working Group in Frankfurt, Saturday, July 18. The proposed resolution currently has status <em>Review</em>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>niels_dekker</dc:creator> <pubDate>Wed, 21 Oct 2009 17:30:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2857#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2857#comment:4</guid> <description> <p> Update: Yesterday, the Standard Library Working Group has discussed my proposal to add <em>c_array()</em> member functions to <em>std::array</em>, in Santa Cruz. Unfortunately: <em>There was not enough consensus that this was sufficiently useful.</em> <a class="ext-link" href="http://home.roadrunner.com/~hinnant/issue_review/lwg-closed.html#930"><span class="icon">​</span>http://home.roadrunner.com/~hinnant/issue_review/lwg-closed.html#930</a> So now it is unlikely that <em>std::array</em> will offer an easy way to get a reference to its internal built-in array. </p> <p> I wonder if it's safe to cast <em>std::array&lt;T,N&gt;</em> to a pointer-to-array, as follows: </p> <pre class="wiki">static_cast&lt;T(*)[N]&gt;(static_cast&lt;void*&gt;(arr.data())); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Sun, 28 Mar 2010 05:22:28 GMT</pubDate> <title>owner changed https://svn.boost.org/trac10/ticket/2857#comment:5 https://svn.boost.org/trac10/ticket/2857#comment:5 <ul> <li><strong>owner</strong> changed from <span class="trac-author">No-Maintainer</span> to <span class="trac-author">Marshall Clow</span> </li> </ul> Ticket Marshall Clow Sun, 28 Mar 2010 05:23:40 GMT status changed https://svn.boost.org/trac10/ticket/2857#comment:6 https://svn.boost.org/trac10/ticket/2857#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> I will keep an eye on this issue; see what the standards committee decides. </p> Ticket niels_dekker Sun, 28 Mar 2010 11:01:25 GMT <link>https://svn.boost.org/trac10/ticket/2857#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2857#comment:7</guid> <description> <p> Replying to comment <a class="ticket" href="https://svn.boost.org/trac10/ticket/2857#comment:6" title="Comment 6">6</a>: </p> <blockquote class="citation"> <p> I will keep an eye on this issue; see what the standards committee decides. </p> </blockquote> <p> Thanks, Marchall. Unfortunately the standard library issue, <a class="ext-link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3020.html#930"><span class="icon">​</span>LWG #930, Access to std::array data as built-in array type</a>, has been rejected. I do not entirely understand why. </p> <p> Anyway, a non-member function like <code>get_c_array(array&lt;T,N&gt;&amp;)</code> (as I suggested here before at comment <a class="ticket" href="https://svn.boost.org/trac10/ticket/2857#comment:2" title="Comment 2">2</a>) would also be fine to me. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Sun, 06 Jun 2010 16:06:40 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/2857#comment:8 https://svn.boost.org/trac10/ticket/2857#comment:8 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.39.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> <p> I've checked get_c_array into the trunk; waiting to see how the tests turn out. </p> Ticket Marshall Clow Fri, 13 Aug 2010 20:50:06 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2857#comment:9 https://svn.boost.org/trac10/ticket/2857#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> get_c_array is part of the 1.44 release. </p> Ticket