Placement syntax: Difference between revisions

Content deleted Content added
Expanded further.
Expanded further.
Line 6:
:<code>new</code> ''new-type-id'' ( ''optional-initializer-expression-list'' )</code>
 
The placement syntax adds an expression list immediately after the <code>new</code> keyword. This expression list is the placement. It can contain any number of expressions.<ref name=Lischner1 /><ref name=Lippman1>{{cite book|title=C++ Gems|author=Stanley B. Lippman|pages=386&ndash;389|publisher=Cambridge University Press|date=1997|isbn=0135705819|isbn13=9780135705810}}</ref><ref name=Loudon1>{{cite book|title=C++ Pocket Reference|author=Kyle Loudon|pages=109&ndash;110|publisher=O'Reilly|date=2003|isbn=0596004966|isbn13=9780596004965}}</ref>
:<code>new</code> ( ''expression-list'' ) ''new-type-id'' ( ''optional-initializer-expression-list'' )</code>
 
Line 75:
</source>
=== Preventing exceptions ===
Normally, the (non-placement) new functions throw an exception, of type <code>std::bad_alloc</code>, if they encounter an error, such as exhaustion of all available memory. This was not how the functions were defined by Stroustrup's ''Annotated C++ Reference Manual'', but was a change made by the standardization committee when the C++ language was standardized. The original behaviour of the functions, which was to return a <source lang="cpp" enclose=none>NULL</source> pointer when an error occurred, is accessible via placement syntax.<ref name=Lippman1 /><ref name=Loudon1 />
 
Programmers that wish to do this in their programs must include the Standard C++ library header <code>&lt;new&gt;</code> in the source code. This header defines the global <code>std::nothrow</code> object, which is of type <code>std::nothrow_t</code> (also defined in the header), which is used to call the overloaded new functions that are declared as taking <source lang="cpp" enclose=none>const nothrow_t &</source> as their second parameter. For example:<ref name=Anderson1 />