| 446 | <h3><a name="object_creation">object creation</a></h3> |
| 447 | <pre>template<class T, class... Args> |
| 448 | shared_ptr<T> make_shared( Args && ... args ); |
| 449 | template<class T, class A, class... Args> |
| 450 | shared_ptr<T> allocate_shared( A const & a, Args && ... args );</pre> |
| 451 | <blockquote> |
| 452 | <p><b>Requires:</b> The expression <code>new( pv ) T( std::forward<Args>(args)... )</code>, |
| 453 | where <code>pv</code> is a <code>void*</code> pointing to storage suitable |
| 454 | to hold an object of type <code>T</code>, |
| 455 | shall be well-formed. <code>A</code> shall be an <em>Allocator</em>, |
| 456 | as described in section 20.1.5 (<stong>Allocator requirements</strong>) of the C++ Standard. |
| 457 | The copy constructor and destructor of <code>A</code> shall not throw.</p> |
| 458 | <p><b>Effects:</b> Allocates memory suitable for an object of type <code>T</code> |
| 459 | and constructs an object in it via the placement new expression <code>new( pv ) T()</code> |
| 460 | or <code>new( pv ) T( std::forward<Args>(args)... )</code>. |
| 461 | <code>allocate_shared</code> uses a copy of <code>a</code> to allocate memory. |
| 462 | If an exception is thrown, has no effect.</p> |
| 463 | <p><b>Returns:</b> A <code>shared_ptr</code> instance that stores and owns the address |
| 464 | of the newly constructed object of type <code>T</code>.</p> |
| 465 | <p><b>Postconditions:</b> <code>get() != 0 && use_count() == 1</code>.</p> |
| 466 | <p><b>Throws:</b> <code>bad_alloc</code>, or an exception thrown from <code>A::allocate</code> |
| 467 | or the constructor of <code>T</code>.</p> |
| 468 | <p><b>Notes:</b> This implementation allocates the memory required for the |
| 469 | returned <code>shared_ptr</code> and an object of type <code>T</code> in a single |
| 470 | allocation. This provides efficiency equivalent to an intrusive smart pointer.</p> |
| 471 | <p>The prototypes shown above are used if your compiler supports rvalue references |
| 472 | and variadic templates. They perfectly forward the <code>args</code> parameters to |
| 473 | the constructors of <code>T</code>.</p> |
| 474 | <p>Otherwise, the implementation will fall back on |
| 475 | forwarding the arguments to the constructors of <code>T</code> as const references. |
| 476 | If you need to pass a non-const reference to a constructor of <code>T</code>, |
| 477 | you may do so by wrapping the parameter in a call to <code>boost::ref</code>. |
| 478 | In addition, you will be |
| 479 | limited to a maximum of 9 arguments (not counting the allocator argument of |
| 480 | allocate_shared).</p> |
| 481 | </blockquote> |