Boost C++ Libraries: Ticket #6554: Compiler error dereferencing multi_array value via an iterator https://svn.boost.org/trac10/ticket/6554 <p> It is currently impossible to dereference/access the values contained in a multi_array via the -&gt; operator from an iterator to the multi_array element. However, use the * operator works. </p> <p> Example: </p> <pre class="wiki">struct data { int value; }; typedef boost::multi_array&lt;data, 2&gt; array_type; array_type a(boost::extents[4][5]); // ERROR: Cannot compile this line a.begin()-&gt;begin()-&gt;value = 7; // Compiles successfully (*a.begin()-&gt;begin()).value = 5; </pre><p> I'm using Visual Studio 2010. This is an error in both debug and release modes. This is the error: </p> <p> boost/multi_array/iterator.hpp(40): error C2528: '-&gt;' : pointer to reference is illegal </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6554 Trac 1.4.3 Bill Buklis Wed, 11 Apr 2012 20:55:25 GMT <link>https://svn.boost.org/trac10/ticket/6554#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6554#comment:1</guid> <description> <p> I've figured out what the problem is: </p> <p> I'm not really sure why (perhaps for MWCW and BCC compatibility?), but operator-&gt; for the iterator uses an indirect struct "operator_arrow_proxy" which takes a reference as the type. The operator_arrow_proxy struct then goes on to use a return type for operator-&gt; that is T*. Since T is a reference and not a normal value_type, this is illegal, just as int&amp;* is an illegal type. I modified the structure so that it properly returns a pointer. </p> <p> Old version: </p> <pre class="wiki">template &lt;class T&gt; struct operator_arrow_proxy { operator_arrow_proxy(T const&amp; px) : value_(px) {} T* operator-&gt;() const { return &amp;value_; } // This function is needed for MWCW and BCC, which won't call operator-&gt; // again automatically per 13.3.1.2 para 8 operator T*() const { return &amp;value_; } mutable T value_; }; </pre><p> Updated code: </p> <pre class="wiki">template &lt;class T&gt; struct operator_arrow_proxy { typedef typename remove_reference&lt;T&gt;::type* pointer; operator_arrow_proxy(T const&amp; px) : value_(px) {} pointer operator-&gt;() const { return &amp;value_; } // This function is needed for MWCW and BCC, which won't call operator-&gt; // again automatically per 13.3.1.2 para 8 operator pointer() const { return &amp;value_; } mutable T value_; }; </pre><p> The differences are the typedef for "pointer" using remove_reference and changing the return types from T* to pointer. I also added an include for "boost/type_traits/remove_reference.hpp". </p> <p> Note: This attached iterator.hpp is modified after the trunk version which includes fixes for the MSVC debug output_iterator issue (see <a class="reopened ticket" href="https://svn.boost.org/trac10/ticket/4874" title="#4874: Bugs: multi_array compile errors using Visual C++ 2010 in debug mode (reopened)">ticket:4874</a>). This fix is not currently in the boost release. So if you use the attached file, then make sure to use the rest of the files from the trunk version. The patch itself does not require the MSVC debug fix and may be applied to the release version. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Bill Buklis</dc:creator> <pubDate>Wed, 11 Apr 2012 20:56:01 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/6554 https://svn.boost.org/trac10/ticket/6554 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">iterator.hpp</span> </li> </ul> <p> Merge with trunk version </p> Ticket