I came across a small standard header file <new>. I have probably not seen its direct use before. Here is the g++ version for those who are interested.
Below part is of my interest:
struct nothrow_t { };
extern const nothrow_t nothrow;
/** If you write your own error handler to be called by @c new, it must
* be of this type. */
typedef void (*new_handler)();
/// Takes a replacement handler as the argument, returns the previous handler.
new_handler set_new_handler(new_handler) throw();
- How
struct nothrow_tand its objectnothroware used by programmers ? Is the object really needed to beextern? - When does
new_handlerused ? - Why all the
operator new/deleteare declared inextern C++block ?
nothrow_tis used to telloperator newto operate in the backwards-compatible "return null on failure rather than throwing an exception" mode.That is, if you see code like this:
that would be
nothrow_tat work. For the relevant sections in the standard start at (as of C++11 N3376) 17.6.4.6 [replacement.functions]/1 and work your way down from there.To answer your specific questions:
Yes, it really does have to be extern, at least according to 18.6 [support.dynamic]/1, which includes:
Moreover, 17.6.2.3 [using.linkage]/1 says "Entities in the C++ standard library have external linkage (3.5)". Functions and classes (e.g.
get_new_handlerandset_new_handlerabove) don't explicitly need to be annotated to have external linkage because they have external linkage by default.new_handleris used when the user overrides the defaultoperator newin use by callingset_new_handler. It is just a function pointer type.operator neware not reserved in C.extern "C++"tells the compiler that it is allowed to do name mangling and other C++ specific things to those functions. This way you can compile one translation unit as C, and one as C++, and link them together in the same binary without worrying that someone in C land defined a function conflicting with the compiler'soperator new.