2002-02-02 18:36:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
							 | 
						
					
						
							
								
									
										
										
										
											2000-07-27 14:27:00 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								<html>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									<head>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<title>scoped_array</title>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									</head>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									<body bgcolor="#ffffff" text="#000000">
							 | 
						
					
						
							
								
									
										
										
										
											2005-09-25 21:49:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<h1><A href="../../index.htm"><img src="../../boost.png" alt="boost.png (6897 bytes)" align="middle" width="277" height="86"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
													border="0"></A>scoped_array class template</h1>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<p>The <b>scoped_array</b> class template stores a pointer to a dynamically 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											allocated array. (Dynamically allocated arrays are allocated with the C++ <b>new[]</b>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											expression.) The array pointed to is guaranteed to be deleted, either on 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											destruction of the <b>scoped_array</b>, or via an explicit <b>reset</b>.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>The <b>scoped_array</b> template is a simple solution for simple needs. It 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											supplies a basic "resource acquisition is initialization" facility, without 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											shared-ownership or transfer-of-ownership semantics. Both its name and 
							 | 
						
					
						
							
								
									
										
										
										
											2005-09-25 21:49:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											enforcement of semantics (by being <a href="../utility/utility.htm#Class_noncopyable">
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												noncopyable</a>) signal its intent to retain ownership solely within the 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											current scope. Because it is <a href="../utility/utility.htm#Class_noncopyable">noncopyable</a>, 
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											it is safer than <b>shared_array</b> for pointers which should not be copied.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Because <b>scoped_array</b> is so simple, in its usual implementation every 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											operation is as fast as a built-in array pointer and it has no more space 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											overhead that a built-in array pointer.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>It cannot be used in C++ standard library containers. See <a href="shared_array.htm">
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												<b>shared_array</b></a> if <b>scoped_array</b> does not meet your needs.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>It cannot correctly hold a pointer to a single object. See <a href="scoped_ptr.htm"><b>scoped_ptr</b></a>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											for that usage.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>A <b>std::vector</b> is an alternative to a <b>scoped_array</b> that is a bit 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											heavier duty but far more flexible. A <b>boost::array</b> is an alternative 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											that does not use dynamic allocation.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>The class template is parameterized on <b>T</b>, the type of the object pointed 
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											to. <b>T</b> must meet the smart pointer <a href="smart_ptr.htm#common_requirements">
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												common requirements</a>.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<h2>Synopsis</h2>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>namespace boost {
							 | 
						
					
						
							
								
									
										
										
										
											2000-07-27 14:27:00 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2003-01-09 13:11:10 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  template<class T> class scoped_array : <a href="../utility/utility.htm#Class_noncopyable">noncopyable</a> {
							 | 
						
					
						
							
								
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    public:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      typedef T <a href="#element_type">element_type</a>;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      explicit <a href="#ctor">scoped_array</a>(T * p = 0); // never throws
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      <a href="#destructor">~scoped_array</a>(); // never throws
							 | 
						
					
						
							
								
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      void <a href="#reset">reset</a>(T * p = 0); // never throws
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      T & <a href="#operator[]">operator[]</a>(std::ptrdiff_t i) const; // never throws
							 | 
						
					
						
							
								
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      T * <a href="#get">get</a>() const; // never throws
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								     
							 | 
						
					
						
							
								
									
										
										
										
											2005-09-25 21:49:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      operator <A href="#conversions" ><i>unspecified-bool-type</i></A>() const; // never throws
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      void <a href="#swap">swap</a>(scoped_array & b); // never throws
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  template<class T> void <a href="#free-swap">swap</a>(scoped_array<T> & a, scoped_array<T> & b); // never throws
							 | 
						
					
						
							
								
									
										
										
										
											2000-07-27 14:27:00 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}</pre>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<h2>Members</h2>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											<a name="element_type">element_type</a></h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>typedef T element_type;</pre>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Provides the type of the stored pointer.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<h3><a name="ctor">constructors</a></h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>explicit scoped_array(T * p = 0); // never throws</pre>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Constructs a <b>scoped_array</b>, storing a copy of <b>p</b>, which must have 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											been allocated via a C++ <b>new</b>[] expression or be 0. <b>T</b> is not 
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											required be a complete type. See the smart pointer <a href="smart_ptr.htm#common_requirements">
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												common requirements</a>.</p>
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<h3><a name="destructor">destructor</a></h3>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<pre>~scoped_array(); // never throws</pre>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Deletes the array pointed to by the stored pointer. Note that <b>delete[]</b> on 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											a pointer with a value of 0 is harmless. The guarantee that this does not throw 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											exceptions depends on the requirement that the deleted array's objects' 
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											destructors do not throw exceptions. See the smart pointer <a href="smart_ptr.htm#common_requirements">
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												common requirements</a>.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<h3><a name="reset">reset</a></h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>void reset(T * p = 0); // never throws</pre>
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											Deletes the array pointed to by the stored pointer and then stores a copy of p, 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											which must have been allocated via a C++ <b>new[]</b> expression or be 0. The 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											guarantee that this does not throw exceptions depends on the requirement that 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											the deleted array's objects' destructors do not throw exceptions. See the smart 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											pointer <a href="smart_ptr.htm#common_requirements">common requirements</a>.</p>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<h3><a name="operator[]">subscripting</a></h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>T & operator[](std::ptrdiff_t i) const; // never throws</pre>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Returns a reference to element <b>i</b> of the array pointed to by the stored 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											pointer. Behavior is undefined and almost certainly undesirable if the stored 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											pointer is 0, or if <b>i</b> is less than 0 or is greater than or equal to the 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											number of elements in the array.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<h3><a name="get">get</a></h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>T * get() const; // never throws</pre>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Returns the stored pointer. <b>T</b> need not be a complete type. See the smart 
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											pointer <a href="smart_ptr.htm#common_requirements">common requirements</a>.</p>
							 | 
						
					
						
							
								
									
										
										
										
											2005-09-25 21:49:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<h3><a name="conversions">conversions</a></h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>operator <i>unspecified-bool-type</i> () const; // never throws</pre>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Returns an unspecified value that, when used in boolean contexts, is equivalent 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											to <code>get() != 0</code>.</p>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<h3><a name="swap">swap</a></h3>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<pre>void swap(scoped_array & b); // never throws</pre>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<p>Exchanges the contents of the two smart pointers. <b>T</b> need not be a 
							 | 
						
					
						
							
								
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											complete type. See the smart pointer <a href="smart_ptr.htm#common_requirements">common 
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												requirements</a>.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<h2><a name="functions">Free Functions</a></h2>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<h3><a name="free-swap">swap</a></h3>
							 | 
						
					
						
							
								
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<pre>template<class T> void swap(scoped_array<T> & a, scoped_array<T> & b); // never throws</pre>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<p>Equivalent to <b>a.swap(b)</b>. Matches the interface of <b>std::swap</b>. 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											Provided as an aid to generic programming.</p>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										<hr>
							 | 
						
					
						
							
								
									
										
										
										
											2005-09-25 21:49:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan--> 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											09 January 2003<!--webbot bot="Timestamp" endspan i-checksum="32310"--></p>
							 | 
						
					
						
							
								
									
										
										
										
											2007-11-25 18:38:02 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										<p><small>Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											Copyright 2002-2005 Peter Dimov. Distributed under the Boost Software License, Version 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											1.0. See accompanying file <A href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-16 15:26:52 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									</body>
							 | 
						
					
						
							
								
									
										
										
										
											2005-09-25 21:49:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								</html>
							 |