Boost C++ Libraries: Ticket #7577: base_from_member with reference types https://svn.boost.org/trac10/ticket/7577 <p> <code>base_from_member</code> constructors take their arguments by value, which causes undefined behavior if the member type is a reference, as a dangling reference is created (unless, I presume, its initialized with <code>boost::ref/cref</code>). </p> <p> The attached patch adds a partial specialization of <code>base_from_member</code> for references. It provides a single constructor that takes its argument by reference. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7577 Trac 1.4.3 K-ballo <kaballo86@…> Fri, 26 Oct 2012 19:18:30 GMT attachment set https://svn.boost.org/trac10/ticket/7577 https://svn.boost.org/trac10/ticket/7577 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">base_from_member.patch</span> </li> </ul> <p> Fix </p> Ticket viboes Mon, 04 Feb 2013 22:32:26 GMT owner, status changed https://svn.boost.org/trac10/ticket/7577#comment:1 https://svn.boost.org/trac10/ticket/7577#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">No-Maintainer</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> Could you show a use case for a base_from_member with a reference and the generated error? </p> Ticket kaballo86@… Mon, 04 Feb 2013 23:24:50 GMT attachment set https://svn.boost.org/trac10/ticket/7577 https://svn.boost.org/trac10/ticket/7577 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">ref_member_from_base.cpp</span> </li> </ul> <p> reference base_from_member issue sample </p> Ticket kaballo86@… Mon, 04 Feb 2013 23:28:01 GMT <link>https://svn.boost.org/trac10/ticket/7577#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:2</guid> <description> <p> I have added an use case with a reference base_from_member resulting in undefined behavior. It's based on my original use case, where I had a view over a pair of iterators and I wanted to implement a view over a container reusing the previous view. When base_from_member is instantiated with a reference, the reference points to the copy of the argument used in its construction, so it effectively becomes a dangling reference. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 05 Feb 2013 17:44:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:3</guid> <description> <p> Initially I didn't see the need to use base_from_member with a reference, as the referenced object is already initialized. IIUC you want that your class container_something should be able to be instantiated either with a Container and then copy it, or with a Container&amp; and then store the reference. I find this usage a little bit bizarre. Could you tell me in which cases do you use Container and when you use Container&amp;? </p> </description> <category>Ticket</category> </item> <item> <author>kaballo86@…</author> <pubDate>Tue, 05 Feb 2013 17:58:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:4</guid> <description> <p> I could tell you more about the specific use cases, but I think you are missing the point. When instantiating a <code>base_from_member</code> with a reference type the member will contain a dangling reference. There is no error, no warning, no assertion; only a crash at runtime if you are lucky enough. </p> <p> A reference <code>base_from_member</code> cannot be properly initialized (I even failed to get it working by using <code>boost::ref/cref</code>). If my fix is rejected, then at least there should be an assert or a big bold note in <code>base_from_member</code> documentation saying that <code>base_from_member</code> cannot be used with reference types. That is unless you could tell me how would I properly initialize a <code>base_from_member</code> instantiated with a reference type. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 05 Feb 2013 18:32:38 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/7577#comment:4" title="Comment 4">kaballo86@…</a>: </p> <blockquote class="citation"> <p> I could tell you more about the specific use cases, but I think you are missing the point. When instantiating a <code>base_from_member</code> with a reference type the member will contain a dangling reference. There is no error, no warning, no assertion; only a crash at runtime if you are lucky enough. </p> </blockquote> <p> No I didn't missed the point. </p> <blockquote class="citation"> <p> A reference <code>base_from_member</code> cannot be properly initialized (I even failed to get it working by using <code>boost::ref/cref</code>). If my fix is rejected, then at least there should be an assert or a big bold note in <code>base_from_member</code> documentation saying that <code>base_from_member</code> cannot be used with reference types. That is unless you could tell me how would I properly initialize a <code>base_from_member</code> instantiated with a reference type. </p> </blockquote> <p> I understand the problem and how your patch solves the issue. </p> <p> Note that I'm not against your patch, I was just curious to know when you use one or the other. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 05 Feb 2013 18:41:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:6</guid> <description> <p> Ok, I was just making sure you didn't miss the actual issue with <code>base_from_member</code>. </p> <p> The reason the member may be either a reference or a copy is simple. The class is a view, which means most of the time it will contain a reference to the actual type is adapting. However, a view could also be adapting another view, and in those cases it should contain a copy to the view rather than forcing the user to define additional named variables for the intermediate views. Then one can chain views the obvious way. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 06 Feb 2013 12:09:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:7</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/7577#comment:6" title="Comment 6">anonymous</a>: </p> <blockquote class="citation"> <p> Ok, I was just making sure you didn't miss the actual issue with <code>base_from_member</code>. </p> <p> The reason the member may be either a reference or a copy is simple. The class is a view, which means most of the time it will contain a reference to the actual type is adapting. However, a view could also be adapting another view, and in those cases it should contain a copy to the view rather than forcing the user to define additional named variables for the intermediate views. Then one can chain views the obvious way. </p> </blockquote> <p> Why it needs to copy the view? A concrete example will help. </p> </description> <category>Ticket</category> </item> <item> <author>kaballo86@…</author> <pubDate>Wed, 06 Feb 2013 17:31:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/7577#comment:7" title="Comment 7">viboes</a>: </p> <blockquote class="citation"> <p> Why it needs to copy the view? A concrete example will help. </p> </blockquote> <p> Because that's the way it's usually done: views are copied. Views don't own their elements, in a way they are already references. Also, this allows chaining views, for instance <code>one_view( another_view( some_container ) )</code> where the intermediate temporary view cannot be taken by reference. </p> <p> This is totally unrelated to the issue with <code>base_from_member</code>, and as such I think it is off-topic. If you want to continue this discussion contact me to my email address, which you should be able to see here. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 14 Apr 2013 20:46:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:9</guid> <description> <p> Please, could you provide a patch for the documentation? </p> </description> <category>Ticket</category> </item> <item> <author>K-ballo <kaballo86@…></author> <pubDate>Mon, 15 Apr 2013 17:21:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:10</guid> <description> <p> I can certainly try. Should that be documentation stating the issue as it stands today, or a reference for the specialization provided by my patch? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 07 Jul 2013 01:07:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:11</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/7577#comment:10" title="Comment 10">K-ballo &lt;kaballo86@…&gt;</a>: </p> <blockquote class="citation"> <p> I can certainly try. Should that be documentation stating the issue as it stands today, or a reference for the specialization provided by my patch? </p> </blockquote> <p> The specialization provided by the patch. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Wed, 11 Jun 2014 23:55:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7577#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7577#comment:12</guid> <description> <p> Merged in <a class="ext-link" href="https://github.com/boostorg/utility/pull/12"><span class="icon">​</span>https://github.com/boostorg/utility/pull/12</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Wed, 11 Jun 2014 23:55:24 GMT</pubDate> <title>cc set https://svn.boost.org/trac10/ticket/7577#comment:13 https://svn.boost.org/trac10/ticket/7577#comment:13 <ul> <li><strong>cc</strong> <span class="trac-author">Andrey.Semashev@…</span> added </li> </ul> Ticket Andrey Semashev Thu, 12 Jun 2014 17:35:05 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7577#comment:14 https://svn.boost.org/trac10/ticket/7577#comment:14 <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> Ticket