<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>2 Years &#187; C++</title>
	<atom:link href="http://2years.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://2years.net</link>
	<description>两年时间，沧海桑田</description>
	<lastBuildDate>Thu, 26 Jan 2012 21:06:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>

   <image>
    <title>2 Years</title>
    <url>http://0.gravatar.com/avatar/9d856527e38dae9cb8a48f6c12442deb.png?s=48</url>
    <link>http://2years.net</link>
   </image>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>boost::enable_shared_from_this的部分实现研究</title>
		<link>http://2years.net/2010/02/02/enable-shared-from-this-research/</link>
		<comments>http://2years.net/2010/02/02/enable-shared-from-this-research/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 08:52:27 +0000</pubDate>
		<dc:creator>sqrabs</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://2years.net/?p=13</guid>
		<description><![CDATA[shared_ptr是boost库实现的几个智能指针中最重要的，其使用引用计数机制来管理资源。 如果一个类由shared_ptr管理，而在这个类的某个方法又需要传出一个指向自身的指针，那么这个指针也必须被shared_ptr所管理。boost库的实现方法为让被管理类继承boost::enable_shared_from_this类，此后通过shared_from_this函数获得指向自身的shared_ptr。 enable_shared_from_this实质上是把一个weak_ptr作为被管理类的成员，其观察资源使用情况，在调用shared_from_this函数时就是通过这个weak_ptr生成了一个shared_ptr。研读enable_shared_from_this机制的实现时，发现以下代码： 105 106 107 108 109 110 111 112 template&#60; class X, class Y, class T &#62; inline void sp_enable_shared_from_this&#40; boost::shared_ptr&#60;X&#62; const * ppx, Y const * py, boost::enable_shared_from_this&#60; T &#62; const * pe &#41; &#123; if&#40; pe != 0 &#41; &#123; pe-&#62;_internal_accept_owner&#40; ppx, const_cast&#60; Y* &#62;&#40; py &#41; &#41;; &#125; &#125; 这个函数就是设置被管理类中的weak_ptr的。那么这个函数什么时候被调用呢？是在shared_ptr的构造函数中： [...]<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">无觅猜您也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F12%2F06%2F%25E6%259B%25B4%25E6%2594%25B9%25E4%25BA%2586blog%25E7%259A%2584feed%25E5%259C%25B0%25E5%259D%2580feed-2years-net%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">更改了Blog的Feed地址:feed.2years.net</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F03%2F02%2F%25E4%25B8%2580%25E6%259C%2588%25E8%25B0%2588%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一月谈</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F01%2F29%2F%25E6%2597%25A0%25E9%25A2%2598%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">无题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F06%2F02%2Fblog-resumed%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Blog恢复了</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>shared_ptr是boost库实现的几个智能指针中最重要的，其使用引用计数机制来管理资源。</p>
<p>如果一个类由shared_ptr管理，而在这个类的某个方法又需要传出一个指向自身的指针，那么这个指针也必须被shared_ptr所管理。boost库的实现方法为让被管理类继承boost::enable_shared_from_this类，此后通过shared_from_this函数获得指向自身的shared_ptr。</p>
<p>enable_shared_from_this实质上是把一个weak_ptr作为被管理类的成员，其观察资源使用情况，在调用shared_from_this函数时就是通过这个weak_ptr生成了一个shared_ptr。研读enable_shared_from_this机制的实现时，发现以下代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>105
106
107
108
109
110
111
112
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #0000ff;color: #577A61;">template</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;color: #577A61;">class</span> X, <span style="color: #0000ff;color: #577A61;">class</span> Y, <span style="color: #0000ff;color: #577A61;">class</span> T <span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;color: #577A61;">inline</span> <span style="color: #0000ff;color: #343832;">void</span> sp_enable_shared_from_this<span style="color: #008000;color: #CCC;">&#40;</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">shared_ptr</span><span style="color: #000080;">&lt;</span>X<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;color: #343832;">const</span> <span style="color: #000040;">*</span> ppx, Y <span style="color: #0000ff;color: #343832;">const</span> <span style="color: #000040;">*</span> py, boost<span style="color: #008080;">::</span><span style="color: #007788;">enable_shared_from_this</span><span style="color: #000080;">&lt;</span> T <span style="color: #000080;">&gt;</span> <span style="color: #0000ff;color: #343832;">const</span> <span style="color: #000040;">*</span> pe <span style="color: #008000;color: #CCC;">&#41;</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
    <span style="color: #0000ff;color: #B83A24;">if</span><span style="color: #008000;color: #CCC;">&#40;</span> pe <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;color: #DDD;">0</span> <span style="color: #008000;color: #CCC;">&#41;</span>
    <span style="color: #008000;color: #CCC;">&#123;</span>
        pe<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>_internal_accept_owner<span style="color: #008000;color: #CCC;">&#40;</span> ppx, <span style="color: #0000ff;color: #577A61;">const_cast</span><span style="color: #000080;">&lt;</span> Y<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;color: #CCC;">&#40;</span> py <span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;color: #CCC;">&#125;</span>
<span style="color: #008000;color: #CCC;">&#125;</span></pre></td></tr></table></div>

<p>这个函数就是设置被管理类中的weak_ptr的。那么这个函数什么时候被调用呢？是在shared_ptr的构造函数中：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>186
187
188
189
190
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #0000ff;color: #577A61;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;color: #577A61;">class</span> Y<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;color: #577A61;">explicit</span> shared_ptr<span style="color: #008000;color: #CCC;">&#40;</span> Y <span style="color: #000040;">*</span> p <span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">:</span> px<span style="color: #008000;color: #CCC;">&#40;</span> p <span style="color: #008000;color: #CCC;">&#41;</span>, pn<span style="color: #008000;color: #CCC;">&#40;</span> p <span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #666666;color: #CDC;">// Y must be complete</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
    boost<span style="color: #008080;">::</span><span style="color: #007788;">detail</span><span style="color: #008080;">::</span><span style="color: #007788;">sp_enable_shared_from_this</span><span style="color: #008000;color: #CCC;">&#40;</span> <span style="color: #0000dd;color: #8FB394;">this</span>, p, p <span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;color: #CCC;">&#125;</span></pre></td></tr></table></div>

<p>这里比较奇怪的地方就是sp_enable_shared_from_this这个函数是始终被调用的，但是如果被管理类没有继承自enable_shared_from_this，那这个调用肯定是通不过的，因为指向普通类型Y的指针p不能转型为指向enable_shared_from_this类的指针。继续翻代码，发现sp_enable_shared_from_this函数的一个重载：<br />
inline void sp_enable_shared_from_this( &#8230; ){}<br />
原来这个地方充分利用了函数重载发现机制，如果被管理类继承自enable_shared_from_this，那么第一个sp_enable_shared_from_this就是最合适的，shared_ptr的构造函数中会调用这个版本。否则，就会调用第二个sp_enable_shared_from_this函数也就是最通用化的版本，因为&#8230;就是不定参数，它可以接受任何传入的类型。而这个参数又是一个空函数，什么事都不会发生。</p>
<p>这种实现方式也避免了效率的降低，因为2个sp_enable_shared_from_this都是内联inline的，这就保证了如果不需要设置weak_ptr那么不会有任何函数调用发生。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">无觅猜您也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F12%2F06%2F%25E6%259B%25B4%25E6%2594%25B9%25E4%25BA%2586blog%25E7%259A%2584feed%25E5%259C%25B0%25E5%259D%2580feed-2years-net%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">更改了Blog的Feed地址:feed.2years.net</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F03%2F02%2F%25E4%25B8%2580%25E6%259C%2588%25E8%25B0%2588%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一月谈</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F12%2F06%2F%25E6%258E%25A8%25E8%258D%2590%25E4%25B8%2580%25E4%25B8%25AA%25E6%2596%25B0%25E7%259A%2584jmx-http%25E6%25A1%25A5%25E6%258E%25A5%25E5%2599%25A8%25EF%25BC%259Ajolokia%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">推荐一个新的JMX-HTTP桥接器：Jolokia</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2F2years.net%2F2010%2F11%2F30%2F%25E6%2596%25B0%25E7%259A%2584vps%25EF%25BC%258Choho%2F&from=http%3A%2F%2F2years.net%2F2010%2F02%2F02%2Fenable-shared-from-this-research%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">新的VPS，hoho</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://2years.net/2010/02/02/enable-shared-from-this-research/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
