This chapter includes the implicit functions and objects generated during the execution of some C + + programs. It also contains the headers for those function, objects and defined types.
This chapter contains sections covering
"18.1 Types" covers predefined types
"18.2 Implementation properties" covers implementation defined properties
"18.3 Start and termination" covers function used for stating and termination of a program
"18.4 Dynamic Memory Management" covers operators used for dynamic allocation and release of memory.
"18.5 Type identification" covers objects and functions used for runtime type identification.
"18.6 Exception Handling" covers objects and functions used for exception handling and errors in exception handling.
"18.7 Other Runtime Support" covers variations from standard C library support functions.
The header <cstddef> contains the same types and definitions as the standard C stddef.h with the following changes as show in "Header <cstddef>".
The headers <limits>, <climits>, and <cfloat> supply implementation dependent characteristics for fundamental types.
The numeric_limits component provides a C + + program with information about various properties o the implementation's representation of the fundamental types.
Specializations including floating point and integer types are provided.
The member is_specialized shall be true for specializations of numeric_limits.
Members declared static const in the numeric_limits template, specializations are usable as integral constant expressions.
Non-fundamental standard types, do not have specializations.
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<wchar_t>;
template<> class numeric_limits<short>;
template<> class numeric_limits<int>;
template<> class numeric_limits<long>;
template<> class numeric_limits<unsigned short>;
template<> class numeric_limits<unsigned int>;
template<> class numeric_limits<unsigned long>;
template<> class numeric_limits<float>;
template<> class numeric_limits<double>;
template<> class numeric_limits<long double>;
// Metrowerks extensions
template<> class numeric_limits<long long>;
template<> class numeric_limits<unsigned long long>;
}
Template class numeric_limits
namespace std {
template<class T> class numeric_limits {
public:
static const bool is_specialized = false;
static T min() throw();
static T max() throw();
static const int digits = 0;
static const int digits10 = 0;
static const bool is_signed = false;
static const bool is_integer = false;
static const bool is_exact = false;
static const int radix = 0;
static T epsilon() throw();
static T round_error() throw();
static const int min_exponent = 0;
static const int min_exponent10 = 0;
static const int max_exponent = 0;
static const int max_exponent10 = 0;
static const bool has_infinity = false;
static const bool has_quiet_NaN = false;
static const bool has_signaling_NaN = false;
static const float_denorm_style has_denorm = denorm_absent;
static const bool has_denorm_loss = false;
static T infinity() throw();
static T quiet_NaN() throw();
static T signaling_NaN() throw();
static T denorm_min() throw();
static const bool is_iec559 = false;
static const bool is_bounded = false;
static const bool is_modulo = false;
static const bool traps = false;
static const bool tinyness_before = false;
static const float_round_style round_style = round_toward_zero;
};
}
18.2.1.2 Numeric_limits Static Members
All static members shall be provided but they do not need to
make sense. see 18.2.1.5
is_specialized
The data member for distinguishing specializations. The default
value is false.
Prototype:
static const bool is_specialized = false;
min
The minimum finite value for floating point types with denormalization.
Prototype:
static T min() throw();
Return:
The maximum positive normalized value is returned.
max
The minimum finite value for floating point types with denormalization.
Prototype:
static T max() throw();
Return:
The maximum positive normalized value is returned.
digits
Designates the number of non-signed digits that can be represented
for integral types. The number of radix digits in the mantissa
for floating point types
Prototype:
static const int digits = 0;
is_signed
Prototype:
static const bool is_signed = false;
is_integer
True if the number is an integer.
Prototype:
static const bool is_integer = false;
is_exact
Prototype:
static const bool is_exact = false;
Remarks:
All integer types are exact, but not all floating point types
are exact.
radix
Specifies the base or radix of the exponent of a floating point
type or base of an integral type.
Prototype:
static const int radix = 0;
epsilon
The difference between 1 and the least value greater than 1.
Prototype:
static T epsilon() throw();
round_error
A function to measure the rounding error.
Prototype:
static T round_error() throw();
Return:
min_exponent
Holds the minimum exponent so that the radix raised to one less
than this would be normalized.
Prototype:
static const int min_exponent;
min_exponent10
Stores the minimum negative exponent that 10 raised to that power
would be a normalized floating point type.
Prototype:
static const int min_exponent10 = 0;
max_exponent
The maximum positive integer so that the radix raised to the
power one less than this is representable.
Prototype:
static const int max_exponent = 0;
max_exponent10
The maximum positive integer so that the 10 raised to this power
is representable.
Prototype:
static const int max_exponent10 = 0;
has_infinity
True if is positive for infinity.
Prototype:
static const bool has_infinity = false;
has_quiet_NaN
True if the number has a quiet "Not a Number".
Prototype:
static const bool has_quiet_NaN = false;
has_signaling_NaN
True if the number is a signaling "Not a Number".
Prototype:
static const bool has_signaling_NaN = false;
has_denorm
Distinguishes if the floating point number has the ability to
be denormalized.
Prototype:
static const float_denorm_style
has_denorm = denorm_absent;
Remarks:
The static variable has_denorm equals denorm_present if the type
allows denormalized values or denorm_absent if the type does
not
and denorm_indeterminate if i is indeterminate
has_denorm_loss
Is true if there is a loss of accuracy because of a denormalization
loss.
Prototype:
static const bool has_denorm_loss = false;
infinity
Determines a positive infinity.
Prototype:
static T infinity() throw();
Return:
Returns a positive infinity if available.
quiet_NaN
Determines if there is a quiet "Not a Number".
Prototype:
static T quiet_NaN() throw();
Return:
Returns a quiet "Not a Number" if available.
signaling_NaN
Determines if there is a signaling "Not a Number".
Prototype:
static T signaling_NaN() throw();
Return:
Returns a signaling "Not a Number" if available.
denorm_min
Determines the minimum positive denormalized value.
Prototype:
static T denorm_min() throw();
Return:
Returns the minimum positive denormalized value.
is_iec559
The values is true if and only if the type adheres to IEC 559
standard
Prototype:
static const bool is_iec559 = false;
is_bounded
The value is true if the set of values representable by the type
is finite.
Prototype:
static const bool is_bounded = false;
Remarks:
All predefined data types are bounded.
is_modulo
This value is true if the type is modulo. A type is modulo if
it is possible to add two positive numbers and have a result
that
wraps around to a third number that is less.
Prototype:
static const bool is_modulo = false;
Remarks:
This value is generally true for unsigned integral types and
false for floating point types.
traps
The value is true if trapping is implemented for the type.
Prototype:
static const bool traps = false;
tinyness_before
This value is true if tinyness is detected before rounding.
Prototype:
static const bool tinyness_before = false;
round_style
This value is the rounding style as a type float_round_style.
Prototype:
static const float_round_style round_style
= round_toward_zero;
See Also:
"Floating Point Rounding Styles"
8.2.1.3 Type float_round_style
An enumerated type in std namespace used to determine the characteristics
for rounding floating point numbers.
Floating Point Rounding Styles:
|
Enumerated Type
|
Value
|
Meaning
|
|---|---|---|
|
|
|
The rounding is indeterminable
|
|
|
|
|
|
|
|
Round is to the nearest value
|
|
|
|
|
|
|
|
|
The presence of denormalization is represented by the std namespace
enumerated type float_denorm_style.
|
Enumerated Type
|
Value
|
Meaning
|
|---|---|---|
|
|
|
Denormalization is indeterminable
|
|
|
|
|
|
|
|
Denormalization is present
|
All member have specializations but these values are not required
to be meaningful. Any value that is not meaningful is set to 0 or false.
The contents of <climits> are the same as standard C's limits.h
and the contents of <cfloat are the same as standard C's float.h.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The header <cfloat> is the same as standard C float.h
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The header <cstdlib> has the same functionality as the standard
C header stdlib.h in regards to start and termination functions
except for the functions and macros as described below.
Start and Termination Differences:
|
|
|
|
|
|
|
|
|
|
|
|
The return from the main function is ignored on the Macintosh operating system and is return
using the native event processing method on other operating systems.
Terminates the Program with abnormal termination.
Prototype:
abort(void)
Remarks:
The program is terminated without executing destructors for objects
of automatic or static storage duration and without calling the
functions passed to atexit.
The atexit function registers functions to be called when exit is called in normal program termination.
Prototype:
extern "C" int atexit(void (* f)(void))
extern "C++" int atexit(void (* f)(void))
Remarks:
If there is no handler for a thrown exception terminate is called The registration of at least 32 functions is allowed.
1. Functions registered with atexit are called in reverse order.
2. A function registered with atexit before an object of static
storage duration will not be called until the objects's destruction.
3. A function registered with atexit after an object of static
storage duration is initialized will be called before the object's
destruction.
Return:
The atexit() function returns zero if the registration succeeds,
non zero if it fails.
Terminates the program with normal cleanup actions.
Prototype:
exit(int status)
Remarks:
The function exit() has additional behavior in order:
1. Objects with static storage duration are destroyed and functions
registered by calling atexit are called.
2. Objects with static storage duration are destroyed in the
reverse order of construction. If main contains no automatic objects
control can be transferred to main if an exception thrown is caught
in main.
3. Functions registered with atexit are called
4. All open C streams with unwritten buffered data are flushed,
closed, including streams associated with cin and cout. All tmpfile() files are removed.
5. Control is returned to the host environment.
If status is zero or EXIT_SUCCESS, a successful termination is
returned to the host environment.
If status is EXIT_FAILURE, an unsuccessful termination is returned
to the host environment.
Otherwise the status returned to the host environment is implementation-defined.
The header <new> defines procedures for the management of dynamic
allocation and error reporting of dynamic allocation errors..
namespace std {
class bad_alloc;
struct nothrow_t {};
extern const nothrow_t nothrow;
typedef void (*new_handler)();
new_handler set_new_handler(new_handler new_p) throw();
}
void* operator new(std::size_t size) throw(std::bad_alloc);
void* operator new(std::size_t size, const std::nothrow_t&) throw();
void operator delete(void* ptr) throw();
void operator delete(void* ptr, const std::nothrow_t&) throw();
void* operator new[](std::size_t size) throw(std::bad_alloc);
void* operator new[](std::size_t size, const std::nothrow_t&) throw();
void operator delete[](void* ptr) throw();
void operator delete[](void* ptr, const std::nothrow_t&) throw();
void* operator new (std::size_t size, void* ptr) throw();
void* operator new[](std::size_t size, void* ptr) throw();
void operator delete (void* ptr, void*) throw();
void operator delete[](void* ptr, void*) throw();
18.4.1 Storage Allocation and Deallocation
This clause covers storage allocation and deallocation functions
and error management.
18.4.1.1 Single Object Forms
Dynamic allocation and freeing of single object data types.
operator new
Dynamically allocates signable objects.
Prototype:
void* operator new
(std::size_t size) throw(std::bad_alloc);
void* operator new
(std::size_t size, const std::nothrow_t&)throw();
Remarks:
The nothrow version of new returns a null pointer on failure. The normal version throws a bad_alloc exception on error.
Return:
Returns a pointer to the allocated memory.
operator delete
Frees memory allocated with operator new.
Prototype:
void operator delete(void* ptr) throw();
void operator delete(void* ptr, const
std::nothrow_t&) throw();
18.4.1.2 Array Forms
Dynamic allocation and freeing of array based data types.
operator new[]
Used for dynamic allocation or array based data types.
Prototype:
void* operator new[]
(std::size_t size) throw(std::bad_alloc);
void* operator new[]
(std::size_t size, const std::nothrow_t&)throw();
Remarks:
The default operator new will throw an exception upon failure.
The nothrow version will return NULL upon failure.
operator delete[]
Operator delete[] is used in conjunction with operator new[]
for array allocations.
Prototype:
void operator delete[]
(void* ptr) throw();
void operator delete[]
(void* ptr, const std::nothrow_t&) throw();
18.4.1.3 Placement Forms
Placement operators are reserved and may not be overloaded by
a C++ program.
placement operator new
Allocates memory at a specific memory address.
Prototype:
void* operator new (std::size_t size, void* ptr)
throw();
void* operator new[](std::size_t size, void* ptr)
throw();
placement operator delete
The placement delete operators are used in conjunction with the
corresponding placement new operators.
Prototype:
void operator delete (void* ptr, void*) throw();
void operator delete[](void* ptr, void*) throw();
18.4.2 Storage Allocation Errors
C++ provides for various objects, functions and types for management
of allocation errors.
18.4.2.1 Class Bad_alloc
A class used to report a failed memory allocation attempt.
namespace std {
class bad_alloc : public exception {
public:
bad_alloc() throw();
bad_alloc(const bad_alloc&) throw();
bad_alloc& operator=(const bad_alloc&) throw();
virtual ~bad_alloc() throw();
virtual const char* what() const throw();
};
}
Constructor
Constructs a bad_alloc object.
Prototype:
bad_alloc() throw();
bad_alloc(const bad_alloc&) throw();
Assignment Operator
Assigns one bad_alloc object to another bad_alloc object.
Prototype:
bad_alloc& operator=(const bad_alloc&) throw();
destructor
Destroys the bad_alloc object.
Prototype:
virtual ~bad_alloc() throw();
what
An error message describing the allocation exception.
Prototype:
virtual const char* what() const throw();
Return:
A null terminated byte string "bad_alloc".
type new_handler
The type of a handler function that is called by operator new
or operator new[].
Prototype:
typedef void (*new_handler)();
If new requires more memory allocation, the new_handler will:
Allocate more memory and return.
Throw an exception of type bad_alloc or bad_alloc derived class.
set_new_handler
Sets the new handler function.
Prototype:
new_handler set_new_handler
(new_handler new_p) throw();
Return:
Zero on the first call and the previous new_handler upon further
calls.
18.5 Type identification
The header <typeinfo> defines three types for type identification
and type identification errors.
Class type_info contains functions and operations to obtain information
about a type.
namespace std {
class type_info {
public:
virtual ~type_info();
bool operator==(const type_info& rhs) const;
bool operator!=(const type_info& rhs) const;
bool before(const type_info& rhs) const;
const char* name() const;
private:
type_info(const type_info& rhs);
type_info& operator=(const type_info& rhs);
};
}
Returns true if types are the same.
Prototype:
bool operator==(const type_info& rhs) const;
Return:
Returns true if the objects are the type.
Prototype:
bool operator!=(const type_info& rhs) const;
Return:
Returns true if the objects are not the same type.
Is true if this object precedes the argument in collation order.
Prototype:
bool before(const type_info& rhs) const;
Returns:
Returns true if *this precedes the argument the collation order.
Returns the name of the class.
Prototype:
const char* name() const;
Return:
Private constructor so copying to prevent copying of this object.
Prototype:
type_info(const type_info& rhs);
Private assignment to prevent copying of this object.
Prototype:
type_info& operator=(const type_info& rhs);
A class for exceptions thrown in runtime casting.
namespace std {
class bad_cast : public exception {
public:
bad_cast() throw();
bad_cast(const bad_cast&) throw();
bad_cast& operator=(const bad_cast&) throw();
virtual ~bad_cast() throw();
virtual const char* what() const throw();
};
}
Constructs an object of class bad_cast.
Prototype:
bad_cast() throw();
bad_cast(const bad_cast&) throw();
Copies an object of class bad_cast.
Prototype:
bad_cast& operator=(const bad_cast&) throw();
An error message describing the casting exception.
Prototype:
virtual const char* what() const throw();
Return:
Returns the null terminated byte string "bad_cast".
Defines a type used for handling bad typeid exceptions.
namespace std {
class bad_typeid : public exception {
public:
bad_typeid() throw();
bad_typeid(const bad_typeid&) throw();
bad_typeid& operator=(const bad_typeid&) throw();
virtual ~bad_typeid() throw();
virtual const char* what() const throw();
};
}
Constructs an object of class bad_typeid.
Prototype:
bad_typeid() throw();
bad_typeid(const bad_typeid&) throw();
Copies a class bad_typeid object.
Prototype:
bad_typeid& operator=(const bad_typeid&) throw();
An error message describing the typeid exception.
Prototype:
virtual const char* what() const throw();
Return:
Returns the null terminated byte string "bad_typeid".
The header <exception> defines types and procedures necessary
for the handling of exceptions.
namespace std {
class exception;
class bad_exception;
typedef void (*unexpected_handler)();
unexpected_handler set_unexpected(unexpected_handler f) throw();
void unexpected();
typedef void (*terminate_handler)();
terminate_handler set_terminate(terminate_handler f) throw();
void terminate();
bool uncaught_exception();
}
A base class for objects thrown as exceptions.
namespace std {
class exception {
public:
exception() throw();
exception(const exception&) throw();
exception& operator=(const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw();
};
}
Constructs and object of the exception class.
Prototype:
exception() throw();
exception(const exception&) throw();
Copies an object of exception class.
Prototype:
exception& operator=(const exception&) throw();
Prototype:
An error message describing the exception.
Prototype:
virtual const char* what() const throw();
Return:
Returns the null terminated byte string "exception".
Defines objects used for exception violations.
A type used for information and reporting of a bad exceptions.
namespace std {
class bad_exception : public exception {
public:
bad_exception() throw();
bad_exception(const bad_exception&) throw();
bad_exception& operator=(const bad_exception&) throw();
virtual ~bad_exception() throw();
virtual const char* what() const throw();
};
}
Constructs an object of class bad_exception.
Prototype:
bad_exception() throw();
bad_exception(const bad_exception&) throw();
Copies an object of class bad_exception
Prototype:
bad_exception& operator=
(const bad_exception&) throw();
An error message describing the bad exception.
Prototype:
virtual const char* what() const throw();
Return:
Returns the null terminated byte string "bad_exception".
A type of handler called by the unexpected function.
Prototype:
typedef void (*unexpected_handler)();
Remarks:
The unexpected_handler calls terminate().
Sets the unexpected handler function.
Prototype:
unexpected_handler set_unexpected
(unexpected_handler f) throw();
Return:
Returns the previous unexpected_handler.
Called when a function ends by an exception not allowed in the
specifications.
Prototype:
void unexpected();
Remarks:
May be called directly by the program.
Types and functions used for abnormal program termination.
A type of handler called by the function terminate when terminating an exception.
Prototype:
typedef void (*terminate_handler)();
Remarks:
The terminate_handler calls abort().
Sets the function for terminating an exception.
Prototype:
terminate_handler set_terminate
(terminate_handler f) throw();
Remarks:
The terminate_handler shall not be a null pointer.
Return:
The previous terminate_handler is returned.
A function called when exception handling is abandoned.
Prototype:
void terminate();
Remarks:
Exception handling may be abandoned by the implementation or
may be called directly by the program.
Determines an uncaught exception
Prototype:
bool uncaught_exception();
Remarks:
Throwing an exception while uncaught_exception is true can result
in a call of terminate.
Return:
Returns true if an exception is uncaught.
The C++ headers <cstdarg>, <csetjmp>, <ctime>, <csignal> and
<cstdlib> contain macros, types and functions that vary from the
corresponding standard C headers.
|
|
|
|
|
|
|
|
|
|
|
|
If the second parameter of va_start is declared with a function,
array, or reference type, or with a type for which there is no
parameter, the behavior is undefined
|
|
|
|
|
|
|
|
|
The function longjmp is ore restricted than in the standard C
implementation.
|
|
|
|
|
|
|
|
|
If a signal handler attempts to use exception handling the result
is undefined.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
All signal handlers should have C linkage.
|
|
|
|
|
|
Last updated: July 21, 2000