¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤ÏưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿(¥Ò¡¼¥×¾å¤Î)¥ª¥Ö¥¸¥§¥¯¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò Êݸ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë¡£¤³¤ì¤ÏC++¥Ó¥ë¥È¥¤¥ó¤Î¥Ý¥¤¥ó¥¿¤Î¤è¤¦¤Ë¿¶Éñ¤¦¤¬¡¢ »Ø¤µ¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤ÏŬÅö¤Ê¤È¤¤Ë¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤ë¡£ ¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤ÏưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤ò¡¢Å¬Åö¤Ê¤È¤¤Ë³Î¼Â¤Ë ¥Ç¥¹¥È¥é¥¯¥È¤·¤¿¤¤¤è¤¦¤Ê¤È¤¤Ë¡¢ÆÃ¤ËÌòΩ¤Ä¡£¤Þ¤¿Ê£¿ô¤Î½êÍ¼Ô¤Ë ¶¦Í¤µ¤ì¤Æ¤¤¤ë¡¢Æ°Åª¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤ò´ÉÍý¤¹¤ë¤Î¤Ë¤â»È¤¨¤ë¡£
³µÇ°Åª¤Ë¤Ï¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤Ï»Ø¤µ¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤ò½êͤ·¤Æ¤¤¤ë¤è¤¦¤Ë¸«¤¨¤ë¡£ ¤½¤·¤Æ¥ª¥Ö¥¸¥§¥¯¥È¤¬¤â¤Ï¤äɬÍפʤ¯¤Ê¤Ã¤¿»þ¡¢ÀÕǤ¤ò»ý¤Ã¤Æºï½ü¤¹¤ë¡£
¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¥é¥¤¥Ö¥é¥ê¤Ï5¼ïÎà¤Î¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¥¯¥é¥¹¥Æ¥ó¥×¥ì¡¼¥È¤òÄ󶡤¹¤ë:
scoped_ptr | <boost/scoped_ptr.hpp> | ¥·¥ó¥×¥ë¤Ç¡¢¤Ò¤È¤Ä¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤Ò¤È¤Ä¤Î½ê͸¢¡£¥³¥Ô¡¼ÉԲġ£ |
scoped_array | <boost/scoped_array.hpp> | ¥·¥ó¥×¥ë¤Ç¡¢ÇÛÎó¤ËÂФ¹¤ë¤Ò¤È¤Ä¤Î½ê͸¢¡£¥³¥Ô¡¼ÉԲġ£ |
shared_ptr | <boost/shared_ptr.hpp> | Ê£¿ô¤Î¥Ý¥¤¥ó¥¿¤Ç¶¦Í¤µ¤ì¤ë¡¢¥ª¥Ö¥¸¥§¥¯¥È¤Î½ê͸¢¡£ |
shared_array | <boost/shared_array.hpp> | Ê£¿ô¤Î¥Ý¥¤¥ó¥¿¤Ç¶¦Í¤µ¤ì¤ë¡¢ÇÛÎó¤Î½ê͸¢¡£ |
weak_ptr | <boost/weak_ptr.hpp> | shared_ptr¤¬½êͤ¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤ò¡¢½êͤ·¤Ê¤¤¤Ç´Ñ»¡¤¹¤ë¡£ |
¤³¤ì¤é¤Î¥Æ¥ó¥×¥ì¡¼¥È¤Ïstd::auto_ptr¥Æ¥ó¥×¥ì¡¼¥È¤òÊä´°¤¹¤ë¤è¤¦¤Ë¥Ç¥¶¥¤¥ó¤µ¤ì¤Æ¤¤¤ë¡£
¤³¤ì¤é¤Ï¡Ö»ñ¸»³ÍÆÀ¤Ï½é´ü²½¤Ç¤¢¤ë¡×¤È¤¤¤¦³Ê¸À¤Î¼ÂÎã¤Ç¤¢¤ë¡£¤³¤Î³Ê¸À¤Ï Bjarne Stroustrup¤Î"The C++ Programming Language"Âè3ÈÇ14.4¾Ï¡¢»ñ¸»´ÉÍý¤Î¹à¤Ç½Ò¤Ù¤é¤ì¤Æ¤¤¤ë¡£
¥Æ¥¹¥È¥×¥í¥°¥é¥àsmart_ptr_test.cpp¤¬ Àµ¤·¤¤Áàºî¤ò³Îǧ¤¹¤ë¤¿¤á¤ËÄ󶡤µ¤ì¤Æ¤¤¤ë¡£
Boost smart pointer¥é¥¤¥Ö¥é¥ê¤Î¸Å¤¤¥Ð¡¼¥¸¥ç¥ó¤Ë¤Ä¤¤¤Æ¤Î¥Ú¡¼¥¸ compatibility¤Ï¡¢ °ÊÁ°¤Î¥Ð¡¼¥¸¥ç¥ó¤Î¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤Î¼ÂÁõ¤«¤é¤ÎÊѹ¹¤Ë¤Ä¤¤¤Æ½Ò¤Ù¤Æ¤¤¤ë¡£
smart pointer timings ¤Ï¥Ñ¥Õ¥©¡¼¥Þ¥ó¥¹¤Î ÌäÂê¤Ë¤Ä¤¤¤Æ¤Î¶½Ì£¤Ç¤¢¤ë¡£
¤³¤ì¤é¤Î¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¥¯¥é¥¹¥Æ¥ó¥×¥ì¡¼¥È¤Ï¥Æ¥ó¥×¥ì¡¼¥È¥Ñ¥é¥á¡¼¥¿T¤ò¤â¤Ä¡£ ¤³¤ì¤Ï¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤¬»Ø¤¹¥ª¥Ö¥¸¥§¥¯¥È¤ÎÊý¤òÆÃÄꤹ¤ë¡£ ¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¥Æ¥ó¥×¥ì¡¼¥È¤Î¿¶¤ëÉñ¤¤¤Ï¡¢T·¿¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Î ¥Ç¥¹¥È¥é¥¯¥¿¤«delete±é»»»Ò¤¬Îã³°¤òÅꤲ¤ë¤Ê¤é¡¢Ì¤ÄêµÁ¤Ç¤¢¤ë¡£
T¤Ï¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤ÎÀë¸À¤È¤¤¤¦ÅÀ¤Ç¡¢ÉÔ´°Á´¤Ê·¿¤«¤â¤·¤ì¤Ê¤¤¡£ Ä̾T¤Ï¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤Î¼ÂÂβ½¤È¤¤¤¦ÅÀ¤Ç´°Á´¤Ê·¿¤Ç¤¢¤ëɬÍפ¬¤¢¤ë¡£ ÉÔ´°Á´¤Ê·¿¤Îºï½ü¤ò´Þ¤á¤Æ¡¢¤³¤ÎÍ×µá¤ò¶¼¤«¤¹¤³¤ÈÁ´¤Æ¤ò¿ÇÃǤ¹¤ë(¥¨¥é¡¼¤È¤·¤Æ°·¤¦) ¤è¤¦¤Ê¼ÂÁõ¤¬É¬ÍפǤ¢¤ë¡£ checked_delete ´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È¤Îµ½Ò¤ò»²¹Í¤Ë¤»¤è¡£
shared_ptr¤Ï¤³¤ÎÀ©¸Â¤ò»ý¤¿¤º¡¢¥á¥ó¥Ð´Ø¿ô¤Î¿¤¯¤ÏT ¤¬´°Á´¤Ê·¿¤Ç¤¢¤ë¤³¤È¤òÍ׵ᤷ¤Ê¤¤¤³¤È¤ËÃí°Õ¤»¤è¡£
T¤Ø¤ÎÍ×µá¤Ï¡¢°ÂÁ´À¤òºÇÂç²½¤·¤Ä¤Ä¡¢°·¤¤¤ä¤¹¤¤¤è¤¦¤Ë¡¢¤È¤¤¤¦³Ê¸À¤Î²¼¤Ç Ãí°Õ¿¼¤¯ºî¤é¤ì¤Æ¤¤¤ë¡£¤³¤Î³Ê¸À¤ÎÃæ¤Ç¤Ï¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤Ï¡¢ T¤¬ÉÔ´°Á´¤Ê·¿¤Ç¤¢¤ëËÝÌõñ°Ì¤ÎÃæ¤Ç¸½¤ì¤ë¡£ ¤³¤ì¤Ï¼ÂÁõ¤È¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤òʬΥ¤·¡¢¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤¬ÍøÍѤµ¤ì¤ëËÝÌõñ°Ì¤«¤é¼ÂÁõ¤ò±£¤¹¡£ ¤½¤ì¤¾¤ì¤Î¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤Ë¤Ä¤¤¤Æ¤Î¤³¤Î¥É¥¥å¥á¥ó¥È¤Ç¤ÎÎã¤Ï¡¢ ¤³¤Î¤è¤¦¤Ê¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¤ÎÍøÍѤòÀâÌÀ¤·¤Æ¤¤¤ë¡£
scoped_ptr¤ÏT¤¬¥Ç¥¹¥È¥é¥¯¥È»þ¤Ë´°Á´¤Ê·¿¤Ç¤¢¤ë¤³¤È¤òÍ׵᤹¤ë¤¬¡¢ shared_ptr¤Ï¤½¤¦¤Ç¤Ê¤¤¤³¤È¤ËÃí°Õ¤»¤è¡£
¤³¤ì¤é¤Î¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¥¯¥é¥¹¤Î¿¤¯¤Î´Ø¿ô¤Ï¡¢¤â¤·Îã³°¤¬Åꤲ¤é¤ì¤ì¤Ð ¡Ö¸ú²Ì¤Ê¤·¡×¤¢¤ë¤¤¤Ï¡Ö¤³¤ì¤³¤ì°Ê³°¤Ï¸ú²Ì¤Ê¤·¡×¤È¤·¤ÆÌÀ¼¨¤µ¤ì¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï¤³¤ì¤é¤Î¥¯¥é¥¹¤Î°ì¤Ä¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬Îã³°¤òÅꤲ¤¿»þ¡¢¥×¥í¥°¥é¥à¤Î ¾õÂÖÁ´¤Æ¤¬¡¢·ë²ÌŪ¤ËÎã³°¤òȯÀ¸¤·¤¿´Ø¿ô¤¬¸Æ¤Ó½Ð¤µ¤ì¤ëÁ°¤Î¾õÂÖ¤ÈÆ±¤¸¤Ç¤¢¤ë ¤³¤È¤ò°ÕÌ£¤·¤Æ¤¤¤ë¡£¤Ä¤Þ¤êȯ¸«¤·¤¦¤ëÉûºîÍѤ¬¤Ê¤¤¤³¤È¤òÊݾڤ·¤Æ¤¤¤ë¡£ ¤½¤¦¤Ç¤Ê¤¤´Ø¿ô¤Ï·è¤·¤ÆÎã³°¤òÅꤲ¤Ê¤¤¡£´Ø¿ô¤¬Åꤲ¤ëÍ£°ì¤ÎÎã³°¤Ï std::bad_alloc¤Ç¤¢¤ë(T¤¬ common requirements¤òËþ¤¿¤·¤Æ¤¤¤ë¤³¤È¤ò²¾Äꤹ¤ë)¡£ std::bad_alloc¤òÅꤲ¤ë²ÄǽÀ¤¬¤¢¤ë¤È¡¢ÌÀ¼¨Åª¤Ë¥É¥¥å¥á¥ó¥È¤Ë½ñ¤«¤ì¤Æ¤¤¤ë ´Ø¿ô¤À¤±¤¬¡¢¤³¤ÎÎã³°¤òÅꤲ¤ë¡£
Exception-specifications¤ÏÍøÍѤµ¤ì¤Ê¤¤¡£ exception-specification rationale¤ò»²¹Í¤Ë¤»¤è¡£
Á´¤Æ¤Î¥¹¥Þ¡¼¥È¥Ý¥¤¥ó¥¿¥Æ¥ó¥×¥ì¡¼¥È¤Ï·è¤·¤ÆÎã³°¤òÅꤲ¤Ê¤¤¥á¥ó¥Ð´Ø¿ô¤ò»ý¤Ã¤Æ¤¤¤ë¡£
·è¤·¤ÆÎã³°¤òÅꤲ¤Ê¤¤¤È¤¤¤¦¤Î¤Ï¡¢¼«Ê¬¼«¿È¤ËÎã³°¤òÅꤲ¤ë¤³¤È¤â¡¢
Îã³°¤òÅꤲ¤ë¾¤Î´Ø¿ô¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤â¤Ê¤¤¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£
¤³¤ì¤é¤Î¥á¥ó¥Ð¤Ï¥³¥á¥ó¥È: //never throws
¤ÈÌÀ¼¨¤µ¤ì¤Æ¤¤¤ë¡£
»Ø¤µ¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È·¿¤òÇ˲õ(destroy)¤¹¤ë´Ø¿ô¤Ï common requirements¤Ë¤è¤Ã¤Æ Îã³°¤òÅꤲ¤ë¤³¤È¤ò¶Ø»ß¤µ¤ì¤Æ¤¤¤ë¡£
January 2002. Peter Dimov reworked all four classes, adding features, fixing bugs, and splitting them into four separate headers, and added weak_ptr. See the compatibility page for a summary of the changes.
May 2001. Vladimir Prus suggested requiring a complete type on destruction. Refinement evolved in discussions including Dave Abrahams, Greg Colvin, Beman Dawes, Rainer Deyke, Peter Dimov, John Maddock, Vladimir Prus, Shankar Sai, and others.
November 1999. Darin Adler provided operator ==, operator !=, and std::swap and std::less specializations for shared types.
September 1999. Luis Coelho provided shared_ptr::swap and shared_array::swap
May 1999. In April and May, 1999, Valentin Bonnard and David Abrahams made a number of suggestions resulting in numerous improvements.
October 1998. In 1994 Greg Colvin proposed to the C++ Standards Committee classes named auto_ptr and counted_ptr which were very similar to what we now call scoped_ptr and shared_ptr. The committee document was 94-168/N0555, Exception Safe Smart Pointers. In one of the very few cases where the Library Working Group's recommendations were not followed by the full committee, counted_ptr was rejected and surprising transfer-of-ownership semantics were added to auto_ptr.
Beman Dawes proposed reviving the original semantics under the names safe_ptr and counted_ptr at an October, 1998, meeting of Per Andersson, Matt Austern, Greg Colvin, Sean Corfield, Pete Becker, Nico Josuttis, Dietmar Kühl, Nathan Myers, Chichiang Wan and Judy Ward. During the discussion, the four class names were finalized, it was decided that there was no need to exactly follow the std::auto_ptr interface, and various function signatures and semantics were finalized.
Over the next three months, several implementations were considered for shared_ptr, and discussed on the boost.org mailing list. The implementation questions revolved around the reference count which must be kept, either attached to the pointed to object, or detached elsewhere. Each of those variants have themselves two major variants:
Each implementation technique has advantages and disadvantages. We went so far as to run various timings of the direct and indirect approaches, and found that at least on Intel Pentium chips there was very little measurable difference. Kevlin Henney provided a paper he wrote on "Counted Body Techniques." Dietmar Kühl suggested an elegant partial template specialization technique to allow users to choose which implementation they preferred, and that was also experimented with.
But Greg Colvin and Jerry Schwarz argued that "parameterization will discourage users", and in the end we choose to supply only the direct implementation.
Revised 4 February 2002
Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.