Boost C++ Libraries: Ticket #6278: mem_fn has a different result_type https://svn.boost.org/trac10/ticket/6278 <p> The following code does not compile: </p> <div class="wiki-code"><div class="code"><pre><span class="cm">/* </span> <span class="cm">&lt;URL: file://localhost/usr/share/doc/packages/boost-1.44.0/libs/bind/mem_fn.html#Purpose &gt;</span> <span class="cm"> All function objects returned by mem_fn expose a result_type typedef</span> <span class="cm"> that represents the return type of the member function. For data</span> <span class="cm"> members, result_type is defined as the type of the member.</span> <span class="cm">*/</span> <span class="k">typedef</span> <span class="kt">int</span> <span class="n">type_of_the_member</span><span class="p">;</span> <span class="k">struct</span> <span class="n">U</span> <span class="p">{</span> <span class="n">type_of_the_member</span> <span class="n">m</span><span class="p">;</span> <span class="p">};</span> <span class="k">template</span> <span class="o">&lt;</span> <span class="k">class</span> <span class="nc">F</span> <span class="o">&gt;</span> <span class="k">typename</span> <span class="n">F</span> <span class="o">::</span><span class="n">result_type</span> <span class="n">U_m</span> <span class="p">(</span><span class="n">F</span> <span class="k">const</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">,</span> <span class="n">U</span> <span class="o">&amp;</span><span class="n">u</span><span class="p">)</span> <span class="p">{</span> <span class="k">typedef</span> <span class="k">typename</span> <span class="n">F</span><span class="o">::</span> <span class="n">result_type</span> <span class="n">r</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">type_of_the_member</span> <span class="n">r</span><span class="p">;</span> <span class="k">typename</span> <span class="n">F</span><span class="o">::</span> <span class="n">result_type</span> <span class="n">r1</span><span class="p">;</span> <span class="k">return</span> <span class="nf">t</span> <span class="p">(</span><span class="n">u</span><span class="p">);</span> <span class="p">}</span> <span class="n">U</span> <span class="n">u</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">x</span> <span class="p">((</span><span class="n">U_m</span> <span class="p">(</span><span class="o">::</span><span class="n">boost</span> <span class="o">::</span><span class="n">mem_fn</span> <span class="p">(</span><span class="o">&amp;</span><span class="n">U</span> <span class="o">::</span><span class="n">m</span><span class="p">),</span> <span class="n">u</span><span class="p">)));</span> </pre></div></div><p> <a class="ext-link" href="http://codepad.org/1op6p2Mc"><span class="icon">​</span>codepad</a> says: <em>error: conflicting declaration 'typedef type_of_the_member r' </em> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6278 Trac 1.4.3 giecrilj@… Fri, 16 Dec 2011 11:55:29 GMT <link>https://svn.boost.org/trac10/ticket/6278#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6278#comment:1</guid> <description> <p> It is unwise for <code>mem_fn</code> to define <code>result_type</code> at all. The type of the member need not even be copyable. Instead, it should be arranged so that </p> <ul><li><code>boost:: result_of &lt; F (U &amp;) &gt;:: type</code> be <code>type_of_the_member &amp;</code> </li><li><code>boost:: result_of &lt; F (U const &amp;) &gt;:: type</code> be <code>type_of_the_member const &amp;</code> </li></ul> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Fri, 16 Dec 2011 13:52:55 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6278#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6278#comment:2</guid> <description> <p> It's true that result_type is defined as a const reference instead of a plain type, but what does this have to do with whether the type is copyable or not? result_of&lt;F(U&amp;)&gt; is not necessarily M&amp;, by the way. U can be a pointer or a smart pointer to a const pointee. What specific problem are you having? </p> </description> <category>Ticket</category> </item> <item> <author>giecrilj@…</author> <pubDate>Fri, 16 Dec 2011 18:50:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6278#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6278#comment:3</guid> <description> <p> If result_type were plain, as it <em>allegedly</em> is, and the type of the member were noncopyable then returning it from a function would be a problem. </p> <p> Specific problem: </p> <ul><li>you cannot copy into a <code>transform_iterator</code> when the transform selects a reference to a member by means of <code>mem_fn</code>. </li></ul><p> If I may digress a bit, I suppose that the reason why the standard does not provide an analogue for <code>mem_fun_ref</code> for data members is that accessing data members is discouraged in C++. Having come to that conclusion, I added the necessary accessors to the underlying class and used just <code>mem_fun_ref</code>, eradicating <code>mem_fn</code> completely. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 22 Oct 2012 22:01:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6278#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6278#comment:4</guid> <description> <p> The error is normal as you have declared twice the type r :( </p> <pre class="wiki"> typedef typename F:: result_type r; typedef type_of_the_member r; </pre> </description> <category>Ticket</category> </item> </channel> </rss>