18 Language support library [language.support]

18.1 General [support.general]

This Clause describes the function signatures that are called implicitly, and the types of objects generated implicitly, during the execution of some C++ programs. It also describes the headers that declare these function signatures and define any related types.

The following subclauses describe common type definitions used throughout the library, characteristics of the predefined types, functions supporting start and termination of a C++ program, support for dynamic memory management, support for dynamic type identification, support for exception processing, support for initializer lists, and other runtime support, as summarized in Table [tab:lang.sup.lib.summary].

Table 29 — Language support library summary
Subclause Header(s)
[support.types] Types <cstddef>
<limits>
[support.limits] Implementation properties <climits>
<cfloat>
[cstdint] Integer types <cstdint>
[support.start.term] Start and termination <cstdlib>
[support.dynamic] Dynamic memory management <new>
[support.rtti] Type identification <typeinfo>
[support.exception] Exception handling <exception>
[support.initlist] Initializer lists <initializer_list>
<csignal>
<csetjmp>
<cstdalign>
[support.runtime] Other runtime support <cstdarg>
<cstdbool>
<cstdlib>
<ctime>

18.2 Types [support.types]

Table [tab:support.hdr.cstddef] describes the header <cstddef>.

Table 30 — Header <cstddef> synopsis
TypeName(s)
Macros: NULL offsetof
Types: ptrdiff_t size_t
max_align_t nullptr_t

The contents are the same as the Standard C library header <stddef.h>, with the following changes:

The macro NULL is an implementation-defined C++ null pointer constant in this International Standard ([conv.ptr]).195

The macro offsetof(type, member-designator) accepts a restricted set of type arguments in this International Standard. If type is not a standard-layout class (Clause [class]), the results are undefined.196 The expression offsetof(type, member-designator) is never type-dependent ([temp.dep.expr]) and it is value-dependent ([temp.dep.constexpr]) if and only if type is dependent. The result of applying the offsetof macro to a field that is a static data member or a function member is undefined. No operation invoked by the offsetof macro shall throw an exception and noexcept(offsetof(type, member-designator)) shall be true.

The type ptrdiff_t is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object, as described in [expr.add].

The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.

Note: It is recommended that implementations choose types for ptrdiff_t and size_t whose integer conversion ranks ([conv.rank]) are no greater than that of signed long int unless a larger size is necessary to contain all the possible values.  — end note ]

The type max_align_t is a POD type whose alignment requirement is at least as great as that of every scalar type, and whose alignment requirement is supported in every context.

nullptr_t is defined as follows:

namespace std {
  typedef decltype(nullptr) nullptr_t;
}

The type for which nullptr_t is a synonym has the characteristics described in [basic.fundamental] and [conv.ptr]. [ Note: Although nullptr's address cannot be taken, the address of another nullptr_t object that is an lvalue can be taken.  — end note ]

See also: Alignment ([basic.align]), Sizeof ([expr.sizeof]), Additive operators ([expr.add]), Free store ([class.free]), and ISO C 7.1.6.

Possible definitions include 0 and 0L, but not (void*)0.

Note that offsetof is required to work as specified even if unary operator& is overloaded for any of the types involved.

18.3 Implementation properties [support.limits]

18.3.1 In general [support.limits.general]

The headers <limits> ([limits]), <climits>, and <cfloat> ([c.limits]) supply characteristics of implementation-dependent arithmetic types ([basic.fundamental]).

18.3.2 Numeric limits [limits]

18.3.2.1 Class template numeric_limits [limits.numeric]

The numeric_limits class template provides a C++ program with information about various properties of the implementation's representation of the arithmetic types.

Specializations shall be provided for each arithmetic type, both floating point and integer, including bool. The member is_specialized shall be true for all such specializations of numeric_limits.

For all members declared static constexpr in the numeric_limits template, specializations shall define these values in such a way that they are usable as constant expressions.

Non-arithmetic standard types, such as complex<T> ([complex]), shall not have specializations.

18.3.2.2 Header <limits> synopsis [limits.syn]

namespace std {
  template<class T> class numeric_limits;
  enum float_round_style;
  enum float_denorm_style;

  template<> class numeric_limits<bool>;

  template<> class numeric_limits<char>;
  template<> class numeric_limits<signed char>;
  template<> class numeric_limits<unsigned char>;
  template<> class numeric_limits<char16_t>;
  template<> class numeric_limits<char32_t>;
  template<> class numeric_limits<wchar_t>;

  template<> class numeric_limits<short>;
  template<> class numeric_limits<int>;
  template<> class numeric_limits<long>;
  template<> class numeric_limits<long long>;
  template<> class numeric_limits<unsigned short>;
  template<> class numeric_limits<unsigned int>;
  template<> class numeric_limits<unsigned long>;
  template<> class numeric_limits<unsigned long long>;

  template<> class numeric_limits<float>;
  template<> class numeric_limits<double>;
  template<> class numeric_limits<long double>;
}

18.3.2.3 Class template numeric_limits [numeric.limits]

namespace std {
  template<class T> class numeric_limits {
  public:
    static constexpr bool is_specialized = false;
    static constexpr T min() noexcept { return T(); }
    static constexpr T max() noexcept { return T(); }
    static constexpr T lowest() noexcept { return T(); }

    static constexpr int  digits = 0;
    static constexpr int  digits10 = 0;
    static constexpr int  max_digits10 = 0;
    static constexpr bool is_signed = false;
    static constexpr bool is_integer = false;
    static constexpr bool is_exact = false;
    static constexpr int  radix = 0;
    static constexpr T epsilon() noexcept { return T(); }
    static constexpr T round_error() noexcept { return T(); }

    static constexpr int  min_exponent = 0;
    static constexpr int  min_exponent10 = 0;
    static constexpr int  max_exponent = 0;
    static constexpr int  max_exponent10 = 0;

    static constexpr bool has_infinity = false;
    static constexpr bool has_quiet_NaN = false;
    static constexpr bool has_signaling_NaN = false;
    static constexpr float_denorm_style has_denorm = denorm_absent;
    static constexpr bool has_denorm_loss = false;
    static constexpr T infinity() noexcept { return T(); }
    static constexpr T quiet_NaN() noexcept { return T(); }
    static constexpr T signaling_NaN() noexcept { return T(); }
    static constexpr T denorm_min() noexcept { return T(); }

    static constexpr bool is_iec559 = false;
    static constexpr bool is_bounded = false;
    static constexpr bool is_modulo = false;

    static constexpr bool traps = false;
    static constexpr bool tinyness_before = false;
    static constexpr float_round_style round_style = round_toward_zero;
  };

  template<class T> class numeric_limits<const T>;
  template<class T> class numeric_limits<volatile T>;
  template<class T> class numeric_limits<const volatile T>;
}

The default numeric_limits<T> template shall have all members, but with 0 or false values.

The value of each member of a specialization of numeric_limits on a cv-qualified type cv T shall be equal to the value of the corresponding member of the specialization on the unqualified type T.

18.3.2.4 numeric_limits members [numeric.limits.members]

static constexpr T min() noexcept;

Minimum finite value.197

For floating types with denormalization, returns the minimum positive normalized value.

Meaningful for all specializations in which is_bounded != false, or is_bounded == false && is_signed == false.

static constexpr T max() noexcept;

Maximum finite value.198

Meaningful for all specializations in which is_bounded != false.

static constexpr T lowest() noexcept;

A finite value x such that there is no other finite value y where y < x.199

Meaningful for all specializations in which is_bounded != false.

static constexpr int digits;

Number of radix digits that can be represented without change.

For integer types, the number of non-sign bits in the representation.

For floating point types, the number of radix digits in the mantissa.200

static constexpr int digits10;

Number of base 10 digits that can be represented without change.201

Meaningful for all specializations in which is_bounded != false.

static constexpr int max_digits10;

Number of base 10 digits required to ensure that values which differ are always differentiated.

Meaningful for all floating point types.

static constexpr bool is_signed;

True if the type is signed.

Meaningful for all specializations.

static constexpr bool is_integer;

True if the type is integer.

Meaningful for all specializations.

static constexpr bool is_exact;

True if the type uses an exact representation. All integer types are exact, but not all exact types are integer. For example, rational and fixed-exponent representations are exact but not integer.

Meaningful for all specializations.

static constexpr int radix;

For floating types, specifies the base or radix of the exponent representation (often 2).202

For integer types, specifies the base of the representation.203

Meaningful for all specializations.

static constexpr T epsilon() noexcept;

Machine epsilon: the difference between 1 and the least value greater than 1 that is representable.204

Meaningful for all floating point types.

static constexpr T round_error() noexcept;

Measure of the maximum rounding error.205

static constexpr int min_exponent;

Minimum negative integer such that radix raised to the power of one less than that integer is a normalized floating point number.206

Meaningful for all floating point types.

static constexpr int min_exponent10;

Minimum negative integer such that 10 raised to that power is in the range of normalized floating point numbers.207

Meaningful for all floating point types.

static constexpr int max_exponent;

Maximum positive integer such that radix raised to the power one less than that integer is a representable finite floating point number.208

Meaningful for all floating point types.

static constexpr int max_exponent10;

Maximum positive integer such that 10 raised to that power is in the range of representable finite floating point numbers.209

Meaningful for all floating point types.

static constexpr bool has_infinity;

True if the type has a representation for positive infinity.

Meaningful for all floating point types.

Shall be true for all specializations in which is_iec559 != false.

static constexpr bool has_quiet_NaN;

True if the type has a representation for a quiet (non-signaling) “Not a Number.”210

Meaningful for all floating point types.

Shall be true for all specializations in which is_iec559 != false.

static constexpr bool has_signaling_NaN;

True if the type has a representation for a signaling “Not a Number.”211

Meaningful for all floating point types.

Shall be true for all specializations in which is_iec559 != false.

static constexpr float_denorm_style has_denorm;

denorm_present if the type allows denormalized values (variable number of exponent bits)212, denorm_absent if the type does not allow denormalized values, and denorm_indeterminate if it is indeterminate at compile time whether the type allows denormalized values.

Meaningful for all floating point types.

static constexpr bool has_denorm_loss;

True if loss of accuracy is detected as a denormalization loss, rather than as an inexact result.213

static constexpr T infinity() noexcept;

Representation of positive infinity, if available.214

Meaningful for all specializations for which has_infinity != false. Required in specializations for which is_iec559 != false.

static constexpr T quiet_NaN() noexcept;

Representation of a quiet “Not a Number,” if available.215

Meaningful for all specializations for which has_quiet_NaN != false. Required in specializations for which is_iec559 != false.

static constexpr T signaling_NaN() noexcept;

Representation of a signaling “Not a Number,” if available.216

Meaningful for all specializations for which has_signaling_NaN != false. Required in specializations for which is_iec559 != false.

static constexpr T denorm_min() noexcept;

Minimum positive denormalized value.217

Meaningful for all floating point types.

In specializations for which has_denorm == false, returns the minimum positive normalized value.

static constexpr bool is_iec559;

True if and only if the type adheres to IEC 559 standard.218

Meaningful for all floating point types.

static constexpr bool is_bounded;

True if the set of values representable by the type is finite.219Note: All fundamental types ([basic.fundamental]) are bounded. This member would be false for arbitrary precision types. — end note ]

Meaningful for all specializations.

static constexpr bool is_modulo;

True if the type is modulo.220 A type is modulo if, for any operation involving +, -, or * on values of that type whose result would fall outside the range [min(),max()], the value returned differs from the true value by an integer multiple of max() - min() + 1.

On most machines, this is false for floating types, true for unsigned integers, and true for signed integers.

Meaningful for all specializations.

static constexpr bool traps;

true if, at program startup, there exists a value of the type that would cause an arithmetic operation using that value to trap.221

Meaningful for all specializations.

static constexpr bool tinyness_before;

true if tinyness is detected before rounding.222

Meaningful for all floating point types.

static constexpr float_round_style round_style;

The rounding style for the type.223

Meaningful for all floating point types. Specializations for integer types shall return round_toward_zero.

Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc.

Equivalent to CHAR_MAX, SHRT_MAX, FLT_MAX, DBL_MAX, etc.

lowest() is necessary because not all floating-point representations have a smallest (most negative) value that is the negative of the largest (most positive) finite value.

Equivalent to FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG.

Equivalent to FLT_DIG, DBL_DIG, LDBL_DIG.

Equivalent to FLT_RADIX.

Distinguishes types with bases other than 2 (e.g. BCD).

Equivalent to FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON.

Rounding error is described in ISO/IEC 10967-1 Language independent arithmetic - Part 1 Section 5.2.8 and Annex A Rationale Section A.5.2.8 - Rounding constants.

Equivalent to FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP.

Equivalent to FLT_MIN_10_EXP, DBL_MIN_10_EXP, LDBL_MIN_10_EXP.

Equivalent to FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP.

Equivalent to FLT_MAX_10_EXP, DBL_MAX_10_EXP, LDBL_MAX_10_EXP.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

See IEC 559.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

International Electrotechnical Commission standard 559 is the same as IEEE 754.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

Refer to IEC 559. Required by LIA-1.

Equivalent to FLT_ROUNDS. Required by LIA-1.

18.3.2.5 Type float_round_style [round.style]

namespace std {
  enum float_round_style {
    round_indeterminate       = -1,
    round_toward_zero         =  0,
    round_to_nearest          =  1,
    round_toward_infinity     =  2,
    round_toward_neg_infinity =  3
  };
}

The rounding mode for floating point arithmetic is characterized by the values:

  • round_indeterminate if the rounding style is indeterminable

  • round_toward_zero if the rounding style is toward zero

  • round_to_nearest if the rounding style is to the nearest representable value

  • round_toward_infinity if the rounding style is toward infinity

  • round_toward_neg_infinity if the rounding style is toward negative infinity

18.3.2.6 Type float_denorm_style [denorm.style]

namespace std {
  enum float_denorm_style {
    denorm_indeterminate = -1,
    denorm_absent = 0,
    denorm_present = 1
  };
}

The presence or absence of denormalization (variable number of exponent bits) is characterized by the values:

  • denorm_indeterminate if it cannot be determined whether or not the type allows denormalized values

  • denorm_absent if the type does not allow denormalized values

  • denorm_present if the type does allow denormalized values

18.3.2.7 numeric_limits specializations [numeric.special]

All members shall be provided for all specializations. However, many values are only required to be meaningful under certain conditions (for example, epsilon() is only meaningful if is_integer is false). Any value that is not “meaningful” shall be set to 0 or false.

Example:

namespace std {
  template<> class numeric_limits<float> {
  public:
    static constexpr bool is_specialized = true;

    inline static constexpr float min() noexcept { return 1.17549435E-38F; }
    inline static constexpr float max() noexcept { return 3.40282347E+38F; }
    inline static constexpr float lowest() noexcept { return -3.40282347E+38F; }

    static constexpr int digits   = 24;
    static constexpr int digits10 =  6;
    static constexpr int max_digits10 =  9;

    static constexpr bool is_signed  = true;
    static constexpr bool is_integer = false;
    static constexpr bool is_exact   = false;

    static constexpr int radix = 2;
    inline static constexpr float epsilon() noexcept     { return 1.19209290E-07F; }
    inline static constexpr float round_error() noexcept { return 0.5F; }

    static constexpr int min_exponent   = -125;
    static constexpr int min_exponent10 = - 37;
    static constexpr int max_exponent   = +128;
    static constexpr int max_exponent10 = + 38;

    static constexpr bool has_infinity             = true;
    static constexpr bool has_quiet_NaN            = true;
    static constexpr bool has_signaling_NaN        = true;
    static constexpr float_denorm_style has_denorm = denorm_absent;
    static constexpr bool has_denorm_loss          = false;

    inline static constexpr float infinity()      noexcept { return value; }
    inline static constexpr float quiet_NaN()     noexcept { return value; }
    inline static constexpr float signaling_NaN() noexcept { return value; }
    inline static constexpr float denorm_min()    noexcept { return min(); }

    static constexpr bool is_iec559  = true;
    static constexpr bool is_bounded = true;
    static constexpr bool is_modulo  = false;
    static constexpr bool traps      = true;
    static constexpr bool tinyness_before = true;

    static constexpr float_round_style round_style = round_to_nearest;
  };
}

 — end example ]

The specialization for bool shall be provided as follows:

namespace std {
   template<> class numeric_limits<bool> {
   public:
     static constexpr bool is_specialized = true;
     static constexpr bool min() noexcept { return false; }
     static constexpr bool max() noexcept { return true; }
     static constexpr bool lowest() noexcept { return false; }

     static constexpr int  digits = 1;
     static constexpr int  digits10 = 0;
     static constexpr int  max_digits10 = 0;

     static constexpr bool is_signed = false;
     static constexpr bool is_integer = true;
     static constexpr bool is_exact = true;
     static constexpr int  radix = 2;
     static constexpr bool epsilon() noexcept { return 0; }
     static constexpr bool round_error() noexcept { return 0; }

     static constexpr int  min_exponent = 0;
     static constexpr int  min_exponent10 = 0;
     static constexpr int  max_exponent = 0;
     static constexpr int  max_exponent10 = 0;

     static constexpr bool has_infinity = false;
     static constexpr bool has_quiet_NaN = false;
     static constexpr bool has_signaling_NaN = false;
     static constexpr float_denorm_style has_denorm = denorm_absent;
     static constexpr bool has_denorm_loss = false;
     static constexpr bool infinity() noexcept { return 0; }
     static constexpr bool quiet_NaN() noexcept { return 0; }
     static constexpr bool signaling_NaN() noexcept { return 0; }
     static constexpr bool denorm_min() noexcept { return 0; }

     static constexpr bool is_iec559 = false;
     static constexpr bool is_bounded = true;
     static constexpr bool is_modulo = false;

     static constexpr bool traps = false;
     static constexpr bool tinyness_before = false;
     static constexpr float_round_style round_style = round_toward_zero;
   };
}

18.3.3 C library [c.limits]

Table [tab:support.hdr.climits] describes the header <climits>.

Table 31 — Header <climits> synopsis
TypeName(s)
Values:
CHAR_BIT INT_MAX LONG_MAX SCHAR_MIN SHRT_MIN ULLONG_MAX
CHAR_MAX LLONG_MAX LONG_MIN SCHAR_MAX UCHAR_MAX ULONG_MAX
CHAR_MIN LLONG_MIN MB_LEN_MAX SHRT_MAX UINT_MAX USHRT_MAX
INT_MIN

The contents are the same as the Standard C library header <limits.h>. [ Note: The types of the constants defined by macros in <climits> are not required to match the types to which the macros refer. — end note ]

Table [tab:support.hdr.cfloat] describes the header <cfloat>.

Table 32 — Header <cfloat> synopsis
TypeName(s)
Values:
DBL_DIG DBL_MIN_EXP FLT_MAX_EXP LDBL_MANT_DIG
DBL_EPSILON DECIMAL_DIG FLT_MIN LDBL_MAX_10_EXP
DBL_MANT_DIG FLT_DIG FLT_MIN_10_EXP LDBL_MAX_EXP
DBL_MAX FLT_EPSILON FLT_MIN_EXP LDBL_MAX
DBL_MAX_10_EXP FLT_EVAL_METHOD FLT_RADIX LDBL_MIN
DBL_MAX_EXP FLT_MANT_DIG FLT_ROUNDS LDBL_MIN_10_EXP
DBL_MIN FLT_MAX LDBL_DIG LDBL_MIN_EXP
DBL_MIN_10_EXP FLT_MAX_10_EXP LDBL_EPSILON

The contents are the same as the Standard C library header <float.h>.

See also: ISO C 7.1.5, 5.2.4.2.2, 5.2.4.2.1.

18.4 Integer types [cstdint]

18.4.1 Header <cstdint> synopsis [cstdint.syn]

namespace std {
  typedef signed integer type int8_t;   // optional
  typedef signed integer type int16_t;  // optional
  typedef signed integer type int32_t;  // optional
  typedef signed integer type int64_t;  // optional

  typedef signed integer type int_fast8_t;
  typedef signed integer type int_fast16_t;
  typedef signed integer type int_fast32_t;
  typedef signed integer type int_fast64_t;

  typedef signed integer type int_least8_t;
  typedef signed integer type int_least16_t;
  typedef signed integer type int_least32_t;
  typedef signed integer type int_least64_t;

  typedef signed integer type intmax_t;
  typedef signed integer type intptr_t;         // optional

  typedef unsigned integer type uint8_t;        // optional
  typedef unsigned integer type uint16_t;       // optional
  typedef unsigned integer type uint32_t;       // optional
  typedef unsigned integer type uint64_t;       // optional

  typedef unsigned integer type uint_fast8_t;
  typedef unsigned integer type uint_fast16_t;
  typedef unsigned integer type uint_fast32_t;
  typedef unsigned integer type uint_fast64_t;

  typedef unsigned integer type uint_least8_t;
  typedef unsigned integer type uint_least16_t;
  typedef unsigned integer type uint_least32_t;
  typedef unsigned integer type uint_least64_t;

  typedef unsigned integer type uintmax_t;
  typedef unsigned integer type uintptr_t;      // optional
} // namespace std

The header also defines numerous macros of the form:

  INT_[FAST LEAST]{8 16 32 64}_MIN
  [U]INT_[FAST LEAST]{8 16 32 64}_MAX
  INT{MAX PTR}_MIN
  [U]INT{MAX PTR}_MAX
  {PTRDIFF SIG_ATOMIC WCHAR WINT}{_MAX _MIN}
  SIZE_MAX

plus function macros of the form:

  [U]INT{8 16 32 64 MAX}_C

The header defines all functions, types, and macros the same as 7.18 in the C standard. [ Note: The macros defined by <cstdint> are provided unconditionally. In particular, the symbols __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS (mentioned in footnotes 219, 220, and 222 in the C standard) play no role in C++.  — end note ]

18.5 Start and termination [support.start.term]

Table [tab:support.hdr.cstdlib] describes some of the contents of the header <cstdlib>.

Table 33 — Header <cstdlib> synopsis
TypeName(s)
Macros: EXIT_FAILURE EXIT_SUCCESS
Functions: _Exit abort atexit
at_quick_exit exit quick_exit

The contents are the same as the Standard C library header <stdlib.h>, with the following changes:

[[noreturn]] void _Exit(int status) noexcept;

The function _Exit(int status) has additional behavior in this International Standard:

  • The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit() ([basic.start.term]).

[[noreturn]] void abort(void) noexcept;

The function abort() has additional behavior in this International Standard:

  • The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit() ([basic.start.term]).

extern "C" int atexit(void (*f)(void)) noexcept; extern "C++" int atexit(void (*f)(void)) noexcept;

Effects: The atexit() functions register the function pointed to by f to be called without arguments at normal program termination. It is unspecified whether a call to atexit() that does not happen before ([intro.multithread]) a call to exit() will succeed. [ Note: The atexit() functions do not introduce a data race ([res.on.data.races]).  — end note ]

Implementation limits: The implementation shall support the registration of at least 32 functions.

Returns: The atexit() function returns zero if the registration succeeds, non-zero if it fails.

[[noreturn]] void exit(int status)

The function exit() has additional behavior in this International Standard:

  • First, objects with thread storage duration and associated with the current thread are destroyed. Next, objects with static storage duration are destroyed and functions registered by calling atexit are called.224 See [basic.start.term] for the order of destructions and calls. (Automatic objects are not destroyed as a result of calling exit().)225

    If control leaves a registered function called by exit because the function does not provide a handler for a thrown exception, std::terminate() shall be called ([except.terminate]).

  • Next, all open C streams (as mediated by the function signatures declared in <cstdio>) with unwritten buffered data are flushed, all open C streams are closed, and all files created by calling tmpfile() are removed.

  • Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.226

extern "C" int at_quick_exit(void (*f)(void)) noexcept; extern "C++" int at_quick_exit(void (*f)(void)) noexcept;

Effects: The at_quick_exit() functions register the function pointed to by f to be called without arguments when quick_exit is called. It is unspecified whether a call to at_quick_exit() that does not happen before ([intro.multithread]) all calls to quick_exit will succeed. [ Note: The at_quick_exit() functions do not introduce a data race ([res.on.data.races]).  — end note ] [ Note: The order of registration may be indeterminate if at_quick_exit was called from more than one thread.  — end note ] [ Note: The at_quick_exit registrations are distinct from the atexit registrations, and applications may need to call both registration functions with the same argument.  — end note ]

Implementation limits: The implementation shall support the registration of at least 32 functions.

Returns: Zero if the registration succeeds, non-zero if it fails.

[[noreturn]] void quick_exit(int status) noexcept;

Effects: Functions registered by calls to at_quick_exit are called in the reverse order of their registration, except that a function shall be called after any previously registered functions that had already been called at the time it was registered. Objects shall not be destroyed as a result of calling quick_exit. If control leaves a registered function called by quick_exit because the function does not provide a handler for a thrown exception, std::terminate() shall be called.Note: at_quick_exit may call a registered function from a different thread than the one that registered it, so registered functions should not rely on the identity of objects with thread storage duration.  — end note ] After calling registered functions, quick_exit shall call _Exit(status). [ Note: The standard file buffers are not flushed. See: ISO C 7.20.4.4.  — end note ]

See also: [basic.start], [basic.start.term], ISO C 7.10.4.

A function is called for every time it is registered.

Objects with automatic storage duration are all destroyed in a program whose function main() contains no automatic objects and executes the call to exit(). Control can be transferred directly to such a main() by throwing an exception that is caught in main().

The macros EXIT_FAILURE and EXIT_SUCCESS are defined in <cstdlib>.

18.6 Dynamic memory management [support.dynamic]

The header <new> defines several functions that manage the allocation of dynamic storage in a program. It also defines components for reporting storage management errors.

Header <new> synopsis

namespace std {
  class bad_alloc;
  class bad_array_new_length;
  struct nothrow_t {};
  extern const nothrow_t nothrow;
  typedef void (*new_handler)();
  new_handler get_new_handler() noexcept;
  new_handler set_new_handler(new_handler new_p) noexcept;
}

void* operator new(std::size_t size);
void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
void  operator delete(void* ptr) noexcept;
void  operator delete(void* ptr, const std::nothrow_t&) noexcept;
void  operator delete(void* ptr, std::size_t size) noexcept;
void  operator delete(void* ptr, std::size_t size,
                      const std::nothrow_t&) noexcept;
void* operator new[](std::size_t size);
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
void  operator delete[](void* ptr) noexcept;
void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;
void  operator delete[](void* ptr, std::size_t size) noexcept;
void  operator delete[](void* ptr, std::size_t size,
                        const std::nothrow_t&) noexcept;

void* operator new  (std::size_t size, void* ptr) noexcept;
void* operator new[](std::size_t size, void* ptr) noexcept;
void  operator delete  (void* ptr, void*) noexcept;
void  operator delete[](void* ptr, void*) noexcept;

See also: [intro.memory], [basic.stc.dynamic], [expr.new], [expr.delete], [class.free], [memory].

18.6.1 Storage allocation and deallocation [new.delete]

Except where otherwise specified, the provisions of ([basic.stc.dynamic]) apply to the library versions of operator new and operator delete.

18.6.1.1 Single-object forms [new.delete.single]

void* operator new(std::size_t size);

Effects: The allocation function ([basic.stc.dynamic.allocation]) called by a new-expression ([expr.new]) to allocate size bytes of storage suitably aligned to represent any object of that size.

Replaceable: a C++ program may define a function with this function signature that displaces the default version defined by the C++ standard library.

Required behavior: Return a non-null pointer to suitably aligned storage ([basic.stc.dynamic]), or else throw a bad_alloc exception. This requirement is binding on a replacement version of this function.

Default behavior:

  • Executes a loop: Within the loop, the function first attempts to allocate the requested storage. Whether the attempt involves a call to the Standard C library function malloc is unspecified.

  • Returns a pointer to the allocated storage if the attempt is successful. Otherwise, if the current new_handler ([get.new.handler]) is a null pointer value, throws bad_alloc.

  • Otherwise, the function calls the current new_handler function ([new.handler]). If the called function returns, the loop repeats.

  • The loop terminates when an attempt to allocate the requested storage is successful or when a called new_handler function does not return.

void* operator new(std::size_t size, const std::nothrow_t&) noexcept;

Effects: Same as above, except that it is called by a placement version of a new-expression when a C++ program prefers a null pointer result as an error indication, instead of a bad_alloc exception.

Replaceable: a C++ program may define a function with this function signature that displaces the default version defined by the C++ standard library.

Required behavior: Return a non-null pointer to suitably aligned storage ([basic.stc.dynamic]), or else return a null pointer. This nothrow version of operator new returns a pointer obtained as if acquired from the (possibly replaced) ordinary version. This requirement is binding on a replacement version of this function.

Default behavior: Calls operator new(size). If the call returns normally, returns the result of that call. Otherwise, returns a null pointer.

Example:

T* p1 = new T;                  // throws bad_alloc if it fails
T* p2 = new(nothrow) T;         // returns nullptr if it fails

 — end example ]

void operator delete(void* ptr) noexcept; void operator delete(void* ptr, std::size_t size) noexcept;

Effects: The deallocation function ([basic.stc.dynamic.deallocation]) called by a delete-expression to render the value of ptr invalid.

Replaceable: a C++ program may define a function with signature void operator delete(void* ptr) noexcept that displaces the default version defined by the C++ standard library. If this function (without size parameter) is defined, the program should also define void operator delete(void* ptr, std::size_t size) noexcept. If this function with size parameter is defined, the program shall also define the version without the size parameter. [ Note: The default behavior below may change in the future, which will require replacing both deallocation functions when replacing the allocation function.  — end note ]

Requires: ptr shall be a null pointer or its value shall be a value returned by an earlier call to the (possibly replaced) operator new(std::size_t) or operator new(std::size_t,const std::nothrow_t&) which has not been invalidated by an intervening call to operator delete(void*).

Requires: If an implementation has strict pointer safety ([basic.stc.dynamic.safety]) then ptr shall be a safely-derived pointer.

Requires: If present, the std::size_t size argument shall equal the size argument passed to the allocation function that returned ptr.

Required behavior: Calls to operator delete(void* ptr, std::size_t size) may be changed to calls to operator delete(void* ptr) without affecting memory allocation. [ Note: A conforming implementation is for operator delete(void* ptr, std::size_t size) to simply call operator delete(ptr).  — end note ]

Default behavior: the function operator delete(void* ptr, std::size_t size) calls operator delete(ptr). [ Note: See the note in the above Replaceable paragraph.  — end note ]

Default behavior: If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.

Remarks: It is unspecified under what conditions part or all of such reclaimed storage will be allocated by subsequent calls to operator new or any of calloc, malloc, or realloc, declared in <cstdlib>.

void operator delete(void* ptr, const std::nothrow_t&) noexcept; void operator delete(void* ptr, std::size_t size, const std::nothrow_t&) noexcept;

Effects: The deallocation function ([basic.stc.dynamic.deallocation]) called by the implementation to render the value of ptr invalid when the constructor invoked from a nothrow placement version of the new-expression throws an exception.

Replaceable: a C++ program may define a function with signature void operator delete(void* ptr, const std::nothrow_t&) noexcept that displaces the default version defined by the C++ standard library. If this function (without size parameter) is defined, the program should also define void operator delete(void* ptr, std::size_t size, const std::nothrow_t&) noexcept. If this function with size parameter is defined, the program shall also define the version without the size parameter. [ Note: The default behavior below may change in the future, which will require replacing both deallocation functions when replacing the allocation function.  — end note ]

Requires: If an implementation has strict pointer safety ([basic.stc.dynamic.safety]) then ptr shall be a safely-derived pointer.

Requires: If present, the std::size_t size argument must equal the size argument passed to the allocation function that returned ptr.

Required behavior: Calls to operator delete(void* ptr, std::size_t size, const std::nothrow_t&) may be changed to calls to operator delete(void* ptr, const std::nothrow_t&) without affecting memory allocation. [ Note: A conforming implementation is for operator delete(void* ptr, std::size_t size, const std::nothrow_t&) to simply call operator delete(void* ptr, const std::nothrow_t&).  — end note ]

Default behavior: operator delete(void* ptr, std::size_t size, const std::nothrow_t&) calls operator delete(ptr, std::nothrow), and operator delete(void* ptr, const std::nothrow_t&) calls operator delete(ptr).

18.6.1.2 Array forms [new.delete.array]

void* operator new[](std::size_t size);

Effects: The allocation function ([basic.stc.dynamic.allocation]) called by the array form of a new-expression ([expr.new]) to allocate size bytes of storage suitably aligned to represent any array object of that size or smaller.227

Replaceable: a C++ program can define a function with this function signature that displaces the default version defined by the C++ standard library.

Required behavior: Same as for operator new(std::size_t). This requirement is binding on a replacement version of this function.

Default behavior: Returns operator new(size).

void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;

Effects: Same as above, except that it is called by a placement version of a new-expression when a C++ program prefers a null pointer result as an error indication, instead of a bad_alloc exception.

Replaceable: a C++ program can define a function with this function signature that displaces the default version defined by the C++ standard library.

Required behavior: Return a non-null pointer to suitably aligned storage ([basic.stc.dynamic]), or return a null pointer. This requirement is binding on a replacement version of this function.

Default behavior: Calls operator new[](size). If the call returns normally, returns the result of that call. Otherwise, returns a null pointer.

void operator delete[](void* ptr) noexcept; void operator delete[](void* ptr, std::size_t size) noexcept;

Effects: The deallocation function ([basic.stc.dynamic.deallocation]) called by the array form of a delete-expression to render the value of ptr invalid.

Replaceable: a C++ program can define a function with signature void operator delete[](void* ptr) noexcept that displaces the default version defined by the C++ standard library. If this function (without size parameter) is defined, the program should also define void operator delete[](void* ptr, std::size_t size) noexcept. If this function with size parameter is defined, the program shall also define the version without the size parameter. [ Note: The default behavior below may change in the future, which will require replacing both deallocation functions when replacing the allocation function.  — end note ]

Requires: ptr shall be a null pointer or its value shall be the value returned by an earlier call to operator new[](std::size_t) or operator new[](std::size_t,const std::nothrow_t&) which has not been invalidated by an intervening call to operator delete[](void*).

Requires: If present, the std::size_t size argument must equal the size argument passed to the allocation function that returned ptr.

Required behavior: Calls to operator delete[](void* ptr, std::size_t size) may be changed to calls to operator delete[](void* ptr) without affecting memory allocation. [ Note: A conforming implementation is for operator delete[](void* ptr, std::size_t size) to simply call operator delete[](void* ptr).  — end note ]

Requires: If an implementation has strict pointer safety ([basic.stc.dynamic.safety]) then ptr shall be a safely-derived pointer.

Default behavior: operator delete[](void* ptr, std::size_t size) calls operator delete[](ptr), and operator delete[](void* ptr) calls operator delete(ptr).

void operator delete[](void* ptr, const std::nothrow_t&) noexcept; void operator delete[](void* ptr, std::size_t size, const std::nothrow_t&) noexcept;

Effects: The deallocation function ([basic.stc.dynamic.deallocation]) called by the implementation to render the value of ptr invalid when the constructor invoked from a nothrow placement version of the array new-expression throws an exception.

Replaceable: a C++ program may define a function with signature void operator delete[](void* ptr, const std::nothrow_t&) noexcept that displaces the default version defined by the C++ standard library. If this function (without size parameter) is defined, the program should also define void operator delete[](void* ptr, std::size_t size, const std::nothrow_t&) noexcept. If this function with size parameter is defined, the program shall also define the version without the size parameter. [ Note: The default behavior below may change in the future, which will require replacing both deallocation functions when replacing the allocation function.  — end note ]

Requires: If an implementation has strict pointer safety ([basic.stc.dynamic.safety]) then ptr shall be a safely-derived pointer.

Requires: If present, the std::size_t size argument must equal the size argument passed to the allocation function that returned ptr.

Required behavior: Calls to operator delete[](void* ptr, std::size_t size, const std::nothrow_t&) may be changed to calls to operator delete[](void* ptr, const std::nothrow_t&) without affecting memory allocation. [ Note: A conforming implementation is for operator delete[](void* ptr, std::size_t size, const std::nothrow_t&) to simply call operator delete[](void* ptr, const std::nothrow_t&).  — end note ]

Default behavior: operator delete[](void* ptr, std::size_t size, const std::nothrow_t&) calls operator delete[](ptr, std::nothrow), and operator delete[](void* ptr, const std::nothrow_t&) calls operator delete[](ptr).

It is not the direct responsibility of operator new[](std::size_t) or operator delete[](void*) to note the repetition count or element size of the array. Those operations are performed elsewhere in the array new and delete expressions. The array new expression, may, however, increase the size argument to operator new[](std::size_t) to obtain space to store supplemental information.

18.6.1.3 Placement forms [new.delete.placement]

These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library ([constraints]). The provisions of ([basic.stc.dynamic]) do not apply to these reserved placement forms of operator new and operator delete.

void* operator new(std::size_t size, void* ptr) noexcept;

Returns: ptr.

Remarks: Intentionally performs no other action.

Example: This can be useful for constructing an object at a known address:

void* place = operator new(sizeof(Something));
Something* p = new (place) Something();

 — end example ]

void* operator new[](std::size_t size, void* ptr) noexcept;

Returns: ptr.

Remarks: Intentionally performs no other action.

void operator delete(void* ptr, void*) noexcept;

Effects: Intentionally performs no action.

Requires: If an implementation has strict pointer safety ([basic.stc.dynamic.safety]) then ptr shall be a safely-derived pointer.

Remarks: Default function called when any part of the initialization in a placement new expression that invokes the library's non-array placement operator new terminates by throwing an exception ([expr.new]).

void operator delete[](void* ptr, void*) noexcept;

Effects: Intentionally performs no action.

Requires: If an implementation has strict pointer safety ([basic.stc.dynamic.safety]) then ptr shall be a safely-derived pointer.

Remarks: Default function called when any part of the initialization in a placement new expression that invokes the library's array placement operator new terminates by throwing an exception ([expr.new]).

18.6.1.4 Data races [new.delete.dataraces]

For purposes of determining the existence of data races, the library versions of operator new, user replacement versions of global operator new, the C standard library functions calloc and malloc, the library versions of operator delete, user replacement versions of operator delete, the C standard library function free, and the C standard library function realloc shall not introduce a data race ([res.on.data.races]). Calls to these functions that allocate or deallocate a particular unit of storage shall occur in a single total order, and each such deallocation call shall happen before ([intro.multithread]) the next allocation (if any) in this order.

18.6.2 Storage allocation errors [alloc.errors]

18.6.2.1 Class bad_alloc [bad.alloc]

namespace std {
  class bad_alloc : public exception {
  public:
    bad_alloc() noexcept;
    bad_alloc(const bad_alloc&) noexcept;
    bad_alloc& operator=(const bad_alloc&) noexcept;
    virtual const char* what() const noexcept;
  };
}

The class bad_alloc defines the type of objects thrown as exceptions by the implementation to report a failure to allocate storage.

bad_alloc() noexcept;

Effects: Constructs an object of class bad_alloc.

Remarks: The result of calling what() on the newly constructed object is implementation-defined.

bad_alloc(const bad_alloc&) noexcept; bad_alloc& operator=(const bad_alloc&) noexcept;

Effects: Copies an object of class bad_alloc.

virtual const char* what() const noexcept;

Returns: An implementation-defined ntbs.

18.6.2.2 Class bad_array_new_length [new.badlength]

namespace std {
  class bad_array_new_length : public bad_alloc {
  public:
    bad_array_new_length() noexcept;
  };
}

The class bad_array_new_length defines the type of objects thrown as exceptions by the implementation to report an attempt to allocate an array of size less than zero or greater than an implementation-defined limit ([expr.new]).

bad_array_new_length() noexcept;

Effects: constructs an object of class bad_array_new_length.

Remarks: the result of calling what() on the newly constructed object is implementation-defined.

18.6.2.3 Type new_handler [new.handler]

typedef void (*new_handler)();

The type of a handler function to be called by operator new() or operator new[]() ([new.delete]) when they cannot satisfy a request for additional storage.

Required behavior: A new_handler shall perform one of the following:

  • make more storage available for allocation and then return;

  • throw an exception of type bad_alloc or a class derived from bad_alloc;

  • terminate execution of the program without returning to the caller;

18.6.2.4 set_new_handler [set.new.handler]

new_handler set_new_handler(new_handler new_p) noexcept;

Effects: Establishes the function designated by new_p as the current new_handler.

Returns: The previous new_handler.

Remarks: The initial new_handler is a null pointer.

18.6.2.5 get_new_handler [get.new.handler]

new_handler get_new_handler() noexcept;

Returns: The current new_handler. [ Note: This may be a null pointer value.  — end note ]

18.7 Type identification [support.rtti]

The header <typeinfo> defines a type associated with type information generated by the implementation. It also defines two types for reporting dynamic type identification errors.

Header <typeinfo> synopsis

namespace std {
  class type_info;
  class bad_cast;
  class bad_typeid;
}

See also: [expr.dynamic.cast], [expr.typeid].

18.7.1 Class type_info [type.info]

namespace std {
  class type_info {
  public:
    virtual ~type_info();
    bool operator==(const type_info& rhs) const noexcept;
    bool operator!=(const type_info& rhs) const noexcept;
    bool before(const type_info& rhs) const noexcept;
    size_t hash_code() const noexcept;
    const char* name() const noexcept;

    type_info(const type_info& rhs) = delete;            // cannot be copied
    type_info& operator=(const type_info& rhs) = delete; // cannot be copied
  };
}

The class type_info describes type information generated by the implementation. Objects of this class effectively store a pointer to a name for the type, and an encoded value suitable for comparing two types for equality or collating order. The names, encoding rule, and collating sequence for types are all unspecified and may differ between programs.

bool operator==(const type_info& rhs) const noexcept;

Effects: Compares the current object with rhs.

Returns: true if the two values describe the same type.

bool operator!=(const type_info& rhs) const noexcept;

Returns: !(*this == rhs).

bool before(const type_info& rhs) const noexcept;

Effects: Compares the current object with rhs.

Returns: true if *this precedes rhs in the implementation's collation order.

size_t hash_code() const noexcept;

Returns: An unspecified value, except that within a single execution of the program, it shall return the same value for any two type_info objects which compare equal.

Remark: an implementation should return different values for two type_info objects which do not compare equal.

const char* name() const noexcept;

Returns: An implementation-defined ntbs.

Remarks: The message may be a null-terminated multibyte string ([multibyte.strings]), suitable for conversion and display as a wstring ([string.classes], [locale.codecvt])

18.7.2 Class bad_cast [bad.cast]

namespace std {
  class bad_cast : public exception {
  public:
    bad_cast() noexcept;
    bad_cast(const bad_cast&) noexcept;
    bad_cast& operator=(const bad_cast&) noexcept;
    virtual const char* what() const noexcept;
  };
}

The class bad_cast defines the type of objects thrown as exceptions by the implementation to report the execution of an invalid dynamic-cast expression ([expr.dynamic.cast]).

bad_cast() noexcept;

Effects: Constructs an object of class bad_cast.

Remarks: The result of calling what() on the newly constructed object is implementation-defined.

bad_cast(const bad_cast&) noexcept; bad_cast& operator=(const bad_cast&) noexcept;

Effects: Copies an object of class bad_cast.

virtual const char* what() const noexcept;

Returns: An implementation-defined ntbs.

Remarks: The message may be a null-terminated multibyte string ([multibyte.strings]), suitable for conversion and display as a wstring ([string.classes], [locale.codecvt])

18.7.3 Class bad_typeid [bad.typeid]

namespace std {
  class bad_typeid : public exception {
  public:
    bad_typeid() noexcept;
    bad_typeid(const bad_typeid&) noexcept;
    bad_typeid& operator=(const bad_typeid&) noexcept;
    virtual const char* what() const noexcept;
  };
}

The class bad_typeid defines the type of objects thrown as exceptions by the implementation to report a null pointer in a typeid expression ([expr.typeid]).

bad_typeid() noexcept;

Effects: Constructs an object of class bad_typeid.

Remarks: The result of calling what() on the newly constructed object is implementation-defined.

bad_typeid(const bad_typeid&) noexcept; bad_typeid& operator=(const bad_typeid&) noexcept;

Effects: Copies an object of class bad_typeid.

virtual const char* what() const noexcept;

Returns: An implementation-defined ntbs.

Remarks: The message may be a null-terminated multibyte string ([multibyte.strings]), suitable for conversion and display as a wstring ([string.classes], [locale.codecvt])

18.8 Exception handling [support.exception]

The header <exception> defines several types and functions related to the handling of exceptions in a C++ program.

Header <exception> synopsis

namespace std {
  class exception;
  class bad_exception;
  class nested_exception;

  typedef void (*unexpected_handler)();
  unexpected_handler get_unexpected() noexcept;
  unexpected_handler set_unexpected(unexpected_handler f) noexcept;
  [[noreturn]] void unexpected();

  typedef void (*terminate_handler)();
  terminate_handler get_terminate() noexcept;
  terminate_handler set_terminate(terminate_handler f) noexcept;
  [[noreturn]] void terminate() noexcept;

  bool uncaught_exception() noexcept;

  typedef unspecified exception_ptr;

  exception_ptr current_exception() noexcept;
  [[noreturn]] void rethrow_exception(exception_ptr p);
  template<class E> exception_ptr make_exception_ptr(E e) noexcept;

  template <class T> [[noreturn]] void throw_with_nested(T&& t);
  template <class E> void rethrow_if_nested(const E& e);
}

See also: [except.special].

18.8.1 Class exception [exception]

namespace std {
  class exception {
  public:
    exception() noexcept;
    exception(const exception&) noexcept;
    exception& operator=(const exception&) noexcept;
    virtual ~exception();
    virtual const char* what() const noexcept;
  };
}

The class exception defines the base class for the types of objects thrown as exceptions by C++ standard library components, and certain expressions, to report errors detected during program execution.

Each standard library class T that derives from class exception shall have a publicly accessible copy constructor and a publicly accessible copy assignment operator that do not exit with an exception. These member functions shall meet the following postcondition: If two objects lhs and rhs both have dynamic type T and lhs is a copy of rhs, then strcmp(lhs.what(), rhs.what()) shall equal 0.

exception() noexcept;

Effects: Constructs an object of class exception.

exception(const exception& rhs) noexcept; exception& operator=(const exception& rhs) noexcept;

Effects: Copies an exception object.

Postcondition: If *this and rhs both have dynamic type exception then strcmp(what(), rhs.what()) shall equal 0.

virtual ~exception();

Effects: Destroys an object of class exception.

virtual const char* what() const noexcept;

Returns: An implementation-defined ntbs.

Remarks: The message may be a null-terminated multibyte string ([multibyte.strings]), suitable for conversion and display as a wstring ([string.classes], [locale.codecvt]). The return value remains valid until the exception object from which it is obtained is destroyed or a non-const member function of the exception object is called.

18.8.2 Class bad_exception [bad.exception]

namespace std {
  class bad_exception : public exception {
  public:
    bad_exception() noexcept;
    bad_exception(const bad_exception&) noexcept;
    bad_exception& operator=(const bad_exception&) noexcept;
    virtual const char* what() const noexcept;
  };
}

The class bad_exception defines the type of objects thrown as described in ([except.unexpected]).

bad_exception() noexcept;

Effects: Constructs an object of class bad_exception.

Remarks: The result of calling what() on the newly constructed object is implementation-defined.

bad_exception(const bad_exception&) noexcept; bad_exception& operator=(const bad_exception&) noexcept;

Effects: Copies an object of class bad_exception.

virtual const char* what() const noexcept;

Returns: An implementation-defined ntbs.

Remarks: The message may be a null-terminated multibyte string ([multibyte.strings]), suitable for conversion and display as a wstring ([string.classes], [locale.codecvt]).

18.8.3 Abnormal termination [exception.terminate]

18.8.3.1 Type terminate_handler [terminate.handler]

typedef void (*terminate_handler)();

The type of a handler function to be called by std::terminate() when terminating exception processing.

Required behavior: A terminate_handler shall terminate execution of the program without returning to the caller.

Default behavior: The implementation's default terminate_handler calls abort().

18.8.3.2 set_terminate [set.terminate]

terminate_handler set_terminate(terminate_handler f) noexcept;

Effects: Establishes the function designated by f as the current handler function for terminating exception processing.

Remarks: It is unspecified whether a null pointer value designates the default terminate_handler.

Returns: The previous terminate_handler.

18.8.3.3 get_terminate [get.terminate]

terminate_handler get_terminate() noexcept;

Returns: The current terminate_handler. [ Note: This may be a null pointer value.  — end note ]

18.8.3.4 terminate [terminate]

[[noreturn]] void terminate() noexcept;

Remarks: Called by the implementation when exception handling must be abandoned for any of several reasons ([except.terminate]), in effect immediately after throwing the exception. May also be called directly by the program.

Effects: Calls the current terminate_handler function. [ Note: A default terminate_handler is always considered a callable handler in this context.  — end note ]

18.8.4 uncaught_exception [uncaught]

bool uncaught_exception() noexcept;

Returns: true after the current thread has initialized an exception object ([except.throw]) until a handler for the exception (including std::unexpected() or std::terminate()) is activated ([except.handle]). [ Note: This includes stack unwinding ([except.ctor]).  — end note ]

Remarks: When uncaught_exception() returns true, throwing an exception can result in a call of std::terminate() ([except.terminate]).

18.8.5 Exception propagation [propagation]

typedef unspecified exception_ptr;

The type exception_ptr can be used to refer to an exception object.

exception_ptr shall satisfy the requirements of NullablePointer ([nullablepointer.requirements]).

Two non-null values of type exception_ptr are equivalent and compare equal if and only if they refer to the same exception.

The default constructor of exception_ptr produces the null value of the type.

exception_ptr shall not be implicitly convertible to any arithmetic, enumeration, or pointer type.

Note: An implementation might use a reference-counted smart pointer as exception_ptr.  — end note ]

For purposes of determining the presence of a data race, operations on exception_ptr objects shall access and modify only the exception_ptr objects themselves and not the exceptions they refer to. Use of rethrow_exception on exception_ptr objects that refer to the same exception object shall not introduce a data race. [ Note: if rethrow_exception rethrows the same exception object (rather than a copy), concurrent access to that rethrown exception object may introduce a data race. Changes in the number of exception_ptr objects that refer to a particular exception do not introduce a data race.  — end note ]

exception_ptr current_exception() noexcept;

Returns: An exception_ptr object that refers to the currently handled exception ([except.handle]) or a copy of the currently handled exception, or a null exception_ptr object if no exception is being handled. The referenced object shall remain valid at least as long as there is an exception_ptr object that refers to it. If the function needs to allocate memory and the attempt fails, it returns an exception_ptr object that refers to an instance of bad_alloc. It is unspecified whether the return values of two successive calls to current_exception refer to the same exception object. [ Note: That is, it is unspecified whether current_exception creates a new copy each time it is called.  — end note ] If the attempt to copy the current exception object throws an exception, the function returns an exception_ptr object that refers to the thrown exception or, if this is not possible, to an instance of bad_exception. [ Note: The copy constructor of the thrown exception may also fail, so the implementation is allowed to substitute a bad_exception object to avoid infinite recursion. — end note ]

[[noreturn]] void rethrow_exception(exception_ptr p);

Requires: p shall not be a null pointer.

Throws: the exception object to which p refers.

template<class E> exception_ptr make_exception_ptr(E e) noexcept;

Effects: Creates an exception_ptr object that refers to a copy of e, as if

try {
  throw e;
} catch(...) {
  return current_exception();
}

Note: This function is provided for convenience and efficiency reasons.  — end note ]

18.8.6 nested_exception [except.nested]

namespace std {
  class nested_exception {
  public:
    nested_exception() noexcept;
    nested_exception(const nested_exception&) noexcept = default;
    nested_exception& operator=(const nested_exception&) noexcept = default;
    virtual ~nested_exception() = default;

    // access functions
    [[noreturn]] void rethrow_nested() const;
    exception_ptr nested_ptr() const noexcept;
  };

  template<class T> [[noreturn]] void throw_with_nested(T&& t);
  template <class E> void rethrow_if_nested(const E& e);
}

The class nested_exception is designed for use as a mixin through multiple inheritance. It captures the currently handled exception and stores it for later use.

Note: nested_exception has a virtual destructor to make it a polymorphic class. Its presence can be tested for with dynamic_cast.  — end note ]

nested_exception() noexcept;

Effects: The constructor calls current_exception() and stores the returned value.

[[noreturn]] void rethrow_nested() const;

Effects: If nested_ptr() returns a null pointer, the function calls std::terminate(). Otherwise, it throws the stored exception captured by *this.

exception_ptr nested_ptr() const noexcept;

Returns: The stored exception captured by this nested_exception object.

template <class T> [[noreturn]] void throw_with_nested(T&& t);

Let U be remove_reference_t<T>.

Requires: U shall be CopyConstructible.

Throws: if U is a non-union class type not derived from nested_exception, an exception of unspecified type that is publicly derived from both U and nested_exception and constructed from std::forward<T>(t), otherwise std::forward<T>(t).

template <class E> void rethrow_if_nested(const E& e);

Effects: If the dynamic type of e is publicly and unambiguously derived from nested_exception, calls dynamic_cast<const nested_exception&>(e).rethrow_nested().

18.9 Initializer lists [support.initlist]

The header <initializer_list> defines a class template and several support functions related to list-initialization (see [dcl.init.list]).

Header <initializer_list> synopsis

namespace std {
  template<class E> class initializer_list {
  public:
    typedef E value_type;
    typedef const E& reference;
    typedef const E& const_reference;
    typedef size_t size_type;

    typedef const E* iterator;
    typedef const E* const_iterator;

    constexpr initializer_list() noexcept;

    constexpr size_t size() const noexcept;      // number of elements
    constexpr const E* begin() const noexcept;   // first element
    constexpr const E* end() const noexcept;     // one past the last element
  };

  // [support.initlist.range] initializer list range access
  template<class E> constexpr const E* begin(initializer_list<E> il) noexcept;
  template<class E> constexpr const E* end(initializer_list<E> il) noexcept;
}

An object of type initializer_list<E> provides access to an array of objects of type const E. [ Note: A pair of pointers or a pointer plus a length would be obvious representations for initializer_list. initializer_list is used to implement initializer lists as specified in [dcl.init.list]. Copying an initializer list does not copy the underlying elements.  — end note ]

18.9.1 Initializer list constructors [support.initlist.cons]

constexpr initializer_list() noexcept;

Effects: constructs an empty initializer_list object.

Postcondition: size() == 0

18.9.2 Initializer list access [support.initlist.access]

constexpr const E* begin() const noexcept;

Returns: A pointer to the beginning of the array. If size() == 0 the values of begin() and end() are unspecified but they shall be identical.

constexpr const E* end() const noexcept;

Returns: begin() + size()

constexpr size_t size() const noexcept;

Returns: The number of elements in the array.

Complexity: Constant time.

18.9.3 Initializer list range access [support.initlist.range]

template<class E> constexpr const E* begin(initializer_list<E> il) noexcept;

Returns: il.begin().

template<class E> constexpr const E* end(initializer_list<E> il) noexcept;

Returns: il.end().

18.10 Other runtime support [support.runtime]

Headers <csetjmp> (nonlocal jumps), <csignal> (signal handling), <cstdalign> (alignment), <cstdarg> (variable arguments), <cstdbool> (__bool_true_false_are_defined). <cstdlib> (runtime environment getenv(), system()), and <ctime> (system clock clock(), time()) provide further compatibility with C code.

The contents of these headers are the same as the Standard C library headers <setjmp.h>, <signal.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stdlib.h>, and <time.h>, respectively, with the following changes:

The restrictions that ISO C places on the second parameter to the va_start() macro in header <stdarg.h> are different in this International Standard. The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...).228 If the parameter parmN is of a reference type, or of a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.

See also: ISO C 4.8.1.1.

The function signature longjmp(jmp_buf jbuf, int val) has more restricted behavior in this International Standard. A setjmp/longjmp call pair has undefined behavior if replacing the setjmp and longjmp by catch and throw would invoke any non-trivial destructors for any automatic objects.

See also: ISO C 7.10.4, 7.8, 7.6, 7.12.

Calls to the function getenv shall not introduce a data race ([res.on.data.races]) provided that nothing modifies the environment. [ Note: Calls to the POSIX functions setenv and putenv modify the environment.  — end note ]

A call to the setlocale function may introduce a data race with other calls to the setlocale function or with calls to functions that are affected by the current C locale. The implementation shall behave as if no library function other than locale::global() calls the setlocale function.

The header <cstdalign> and the header <stdalign.h> shall not define a macro named alignas.

The header <cstdbool> and the header <stdbool.h> shall not define macros named bool, true, or false.

A call to the function signal synchronizes with any resulting invocation of the signal handler so installed.

The common subset of the C and C++ languages consists of all declarations, definitions, and expressions that may appear in a well formed C++ program and also in a conforming C program. A POF (“plain old function”) is a function that uses only features from this common subset, and that does not directly or indirectly use any function that is not a POF, except that it may use plain lock-free atomic operations. A plain lock-free atomic operation is an invocation of a function f from Clause [atomics], such that f is not a member function, and either f is the function atomic_is_lock_free, or for every atomic argument A passed to f, atomic_is_lock_free(A) yields true. All signal handlers shall have C linkage. The behavior of any function other than a POF used as a signal handler in a C++ program is implementation-defined.229

Table 34 — Header <csetjmp> synopsis
TypeName(s)
Macro: setjmp
Type: jmp_buf
Function: longjmp

Table 35 — Header <csignal> synopsis
TypeName(s)
Macros: SIGABRT SIGILL SIGSEGV SIG_DFL
SIG_IGN SIGFPE SIGINT SIGTERM SIG_ERR
Type: sig_atomic_t
Functions: raise signal

Table 36 — Header <cstdalign> synopsis
TypeName(s)
Macro: __alignas_is_defined

Table 37 — Header <cstdarg> synopsis
TypeName(s)
Macros: va_arg va_end va_start
va_copy
Type: va_list

Table 38 — Header <cstdbool> synopsis
TypeName(s)
Macro: __bool_true_false_are_defined

Table 39 — Header <cstdlib> synopsis
TypeName(s)
Functions: getenv system

Table 40 — Header <ctime> synopsis
TypeName(s)
Macro: CLOCKS_PER_SEC
Type: clock_t
Function: clock

Note that va_start is required to work as specified even if unary operator& is overloaded for the type of parmN.

In particular, a signal handler using exception handling is very likely to have problems. Also, invoking std::exit may cause destruction of objects, including those of the standard library implementation, which, in general, yields undefined behavior in a signal handler (see [intro.execution]).