Chapter Contents

Previous

Next
Standard Libraries

Header Files

Most C++ programs need to access some of the classes and functions contained in the libraries provided with the SAS/C C++ Development System. These classes and functions are declared in header files. In addition, you can have header files of your own. Using the SAS/C C++ Development System under TSO, CMS, OS/390 Batch, and UNIX System Services , describes how header files are stored and named under your operating system. Here are some other tips to keep in mind when you use header files in C++ programs:


Header Files Supplied with the Standard Libraries

The following header files are supplied with the standard libraries:

bsamstr.h
<cerrno>
<clocale>
<csetjmp>
<cstddef>
<cstring>
<exception>
iostream.h
stream.h
<typeinfo>

The contents of the bsamstr.h, complex.h, fstream.h, iomanip.h, iostream.h, stdiostream.h, and strstream.h header files are described in I/O Class Descriptions. The contents of stream.h are described in stream.h header file.


The <exception> Header File

The <exception> header declares classes, functions, and types used for exception handling in C++. The contents of <exception> are as follows:

std::set_unexpected, std::unexpected_handler, and std::unexpected

Provides for unexpected exception handling.

SYNOPSIS
namespace std {
    typedef void (*unexpected_handler)();
    unexpected_handler set_unexpected(unexpected_handler) throw();
    void  unexpected();
    }

DESCRIPTION
These functions and types are part of the mechanism used to handle exceptions that do not match the exception specification of a function that is being exited due to exception unwinding.

std::unexpected_handler
This is the type for a pointer to a handler function called when std::unexpected is invoked.

old_handler = std::set_unexpected(new_handler);
This function sets the current unexpected exception handler and returns the previous one. The new_handler pointer must not be NULL. The handler function may not return but may throw an exception or end the program.

std::unexpected
This function calls the current unexpected exception handler. This is effectively called by the exception unwinding mechanism when the exception being thrown does not match the exception specification for a function that is being unwound. The default handler calls std::terminate.

std::set_terminate, std::terminate_handler, and std::terminate

Provides termination handling for exceptions.

SYNOPSIS
namespace std {
   typedef void (*terminate_handler)();
   terminate_handler set_terminate(terminate_handler new_handler) throw();
   void terminate();
   }

DESCRIPTION
These functions and types are part of the mechanism used to terminate the program when normal exception handling fails.

std::terminate_handler
This type represents a handler function. A handler function is called when std::terminate() is called. The handler function must end the program without returning.

old_handler = std::set_terminate(new_handler);
This function sets a user-specified terminate handler that can be used to perform cleanup before terminating program execution. The new handler pointer must not be NULL. The old handler value is returned.

std::terminate();
This function calls the termination handler. This function is effectively called by the C++ exception handler under various conditions when an exception cannot be handled. These conditions include the following:

std::uncaught_exception

Check for pending uncaught exceptions.

SYNOPSIS
namespace std {
    bool uncaught_exception();
    }

DESCRIPTION
This function can be called to determine if the stack is being unwound because of an uncaught exception. During unwinding, throwing an exception may cause a user function invoked by stack unwinding to exit via a throw. In such situations std::terminate() will be called. This function can be called to determine if such a situation might exist. Only the current coprocess is considered.

class std::exception

Base class for C++ run time and library exceptions.

SYNOPSIS
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();
   };
}

DESCRIPTION
This is the base class used for exceptions thrown by the C++ library and run-time support.

CONSTRUCTORS
This class has the standard public default and copy constructors.

DESTRUCTORS
The destructor is virtual and public.

ASSIGNMENT OPERATOR
The assignment operator is public. The object is unchanged.

VIRTUAL MEMBER FUNCTIONS
virtual const char* what() const throw();
This function returns a character string describing the exception type. Unless overridden in a derived class this string is std::exception or derived class.

STATIC MEMBER FUNCTIONS (SAS/C EXTENSION)
static void xtrace( __remote void (*writer)(const char *line) = 0 );
This function dumps traceback information saved from the point of the original throw for the currently handled exception. If the function pointer is NULL, the output is sent to stderr unless the SPE library is being used. See Handling Exceptions in SAS/C for more information.

 static bool is_xtraced();

This function is used to determine if xtrace information has been output. It returns true if the information for the currently handled exception was dumped by =xtrace or an xtrace() call.

class std::bad_exception

Class for reporting unexpected exceptions.

SYNOPSIS
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();
   };
}

DESCRIPTION
This is the class of the object that is thrown by the C++ exception handling mechanism when the exception thrown by an unexpected handler is not allowed by the exception specification of the function being unwound. The object is used to replace the current exception.

CONSTRUCTORS
This class has the standard public default and copy constructors.

DESTRUCTORS
The destructor is virtual and public.

ASSIGNMENT OPERATOR
The assignment operator is public. The object is unchanged.

VIRTUAL MEMBER FUNCTIONS
virtual const char* what() const throw();
This function returns a character string describing the exception type. Unless overridden in a derived class, this string is as follows:
std::bad_exception 
or
derived class
.


The <new> Header File

The <new> header declares the classes, functions, and types used for dynamic memory allocation in the C++ library. The contents of <new> are as follows:

class std::bad_alloc

Class for reporting allocation errors.

SYNOPSIS
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();
   };
}

DESCRIPTION
This class is declared by including <new> or <new.h>. It is used as an exception type for memory allocation errors. The default new operators report memory allocation failure by throwing an object of this class or a derived class.

CONSTRUCTORS
This class has the standard public default and copy constructors.

DESTRUCTORS
The destructor is virtual and public.

ASSIGNMENT OPERATOR
The assignment operator is public. The object is unchanged.

VIRTUAL MEMBER FUNCTIONS

virtual const char* what() const throw();
This function returns a character string describing the exception type. Unless overridden in a derived class this string is std::bad_alloc or derived class.

std::nothrow_t and std::nothrow

SYNOPSIS
namespace std {
    struct nothrow_t {};
    extern const nothrow_t nothrow;
    }

DESCRIPTION
std::nothrow_t
This is simply an empty class.

std::nothrow
This is a library-supplied object that can be used as a placement operand in a new-expression to select the non-throwing version of operator new or operator new[], which are described below.

void* operator new(std::size_t size) throw(std::bad_alloc);
Allocates a block of dynamic memory of size bytes. If insufficient free dynamic memory is available and a new-handler is currently defined, the new-handler is called. If the new-handler returns, operator new tries again to allocate memory, and the process repeats. The new-handler may also exit via throwing an exception of type std::bad_alloc or some class publicly derived from it, in which case operator new exits with the exception. If insufficient memory is available and there is no new-handler, operator new exits by throwing an exception of type std::bad_alloc. This function is user-replaceable. It should never return a NULL pointer.

void* operator new(std::size_t size, const std::nothrow_t&) throw();
Allocates a block of dynamic memory of size bytes or returns NULL. If insufficient free dynamic memory is available and a new-handler is currently defined, the new-handler is called. If the new-handler returns, then operator new tries again to allocate memory, and the process repeats. The new-handler may also exit via throwing an exception of type std::bad_alloc or some class publicly derived from it, in which case operator new returns a NULL pointer. If insufficient memory is available and there is no new-handler, operator new returns NULL. This function is user-replaceable. A pointer returned from this version of the operator is required to be equivalent to a pointer obtained from the standard version.

void operator delete(void* ptr) throw(); void operator delete(void* ptr, std::nothrow_t&) throw();
Frees the block of dynamic memory pointed to by ptr. These functions are equivalent and should accept pointers from both the standard and non-throwing versions of operator new as well as NULL pointers. Each function is user-replaceable.

void* operator new[]( std::size_t bytes ) throw(std::bad_alloc); void* operator new[]( std::size_t bytes, const std::nothrow_t& ) throw();
These are user-replaceable array versions of the preceding operator new declarations. The requirements on these functions are the same as the corresponding non-array versions. The default library versions call the corresponding non-array version.

void operator delete[]( void* pointer ) throw(); void operator delete[]( void* pointer, const std::nothrow_t& ) throw();
These are user-replaceable deletion operators corresponding to the preceding operator new[] declarations. The requirements on these functions are the same as the corresponding non-array delete operators. The default library versions call the corresponding non-array version.

void* operator new(std::size_t size, void* location) throw(); void* operator new[](std::size_t size, void* location) throw();
Does not allocate memory, but instead ignores the size argument and simply returns the location argument as its result. This version of operator new is used to construct objects at a specified location.

void operator delete(void* ptr, void* location) throw(); void operator delete[](void* ptr, void* location) throw();
Does nothing. These are the placement delete forms corresponding to the preceding operator new and operator new[].

std::new_handler and std::set_new_handler

SYNOPSIS
namespace std {
   typedef void (*new_handler)();
   new_handler set_new_handler(new_handler);
   }

DESCRIPTION

std::new_handler
This type describes a pointer to a handler function for std::set_new_handler().
std::set_new_handler
Is used to designate a user-defined function (a new-handler) to handle out-of-memory conditions detected by operator new. The new-handler is called if an operator new function cannot allocate any dynamic memory. The new-handler function takes no arguments and returns void. The new-handler should free some memory before returning to its caller or throw an exception of type std::bad_alloc or a class derived from it; otherwise, an endless loop could result. If it cannot free any memory, it should throw an exception or terminate the program via exit, std::terminate, or abort. Another alternative for the function is to remove itself as a new-handler by calling std::set_new_handler with NULL or a pointer to some other handler. std::set_new_handler returns the previous new-handler or NULL if there was not a previous new-handler.


The new.h Header File

The <new.h> header file provides compatibility with older C++ code. It includes <new> and makes the set_new_handler function visible in the global scope via a using declaration.


The <typeinfo> Header File

The class, functions, and types declared in the header file are provided to identify and compare types, and to specify run time error handling for Run Time Type Identification, or RTTI. The classes std::type_info, std::bad_cast, std::bad_typeid, and std::__non_rtti are declared in this header file. A description of the functions, operators, and types found in these classes is included in this section.

See "Run-Time Type Identification Requirements" in "Appendix 4" in the SAS/C C++ Development System User's Guidefor more information on rtti.

class std::type_info

Provides information on object types.

SYNOPSIS
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;
    };
}

DESCRIPTION
The RTTI operator typeid() returns a reference to a const object of this type. Such objects are created by the C++ implementation to provide an identifier for types. The class allows two std::type_info objects to be compared for type equivalence, or compared for order in an arbitrary collating sequence of types. It also allows a printable description of the represented type to be retrieved.

CONSTRUCTORS
This class has no public constructors.

DESTRUCTORS
The destructor is virtual, so the class is polymorphic itself.

NONVIRTUAL MEMBER FUNCTIONS

bool operator==(const type_info& rhs) const;
Returns true if the two type_info objects represent the same type, and returns false otherwise.

bool operator!=(const type_info& rhs) const;
Returns false if the two type_info objects represent the same type, and returns true otherwise.

bool before(const type_info& rhs) const;
Returns true if the type represented by this object precedes the type represented by rhs in an arbitrary collating sequence of types, and returns false otherwise. This order is arbitrary and may change in different program loads.

const char* name() const;
Returns a pointer to a character string containing a human readable representation of the type represented by the type_info object. The function calls operator new to allocate working storage. The result is cached, so it should not be deallocated by the user.

class std::bad_cast

Class for reporting dynamic_cast errors.

SYNOPSIS
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();
   };
}

DESCRIPTION
This class is used to report invalid dynamic_cast operations. It indicates a dynamic_cast to a reference type when the object could not be cast to the specified type.

CONSTRUCTORS
This class has the standard public default and copy constructors.

DESTRUCTORS
The destructor is virtual and public.

ASSIGNMENT OPERATOR
The assignment operator is public. The object is unchanged.

VIRTUAL MEMBER FUNCTIONS

virtual const char* what() const throw();
Returns a character string describing the exception type. Unless overridden in a derived class, this string is std::bad_cast or derived class.

class std::bad_typeid

Class for reporting typeid operator errors.

SYNOPSIS
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();
   };
}

DESCRIPTION
This class is used to report invalid uses of the typeid operator. It is thrown when the typeid operator is applied to an lvalue expression of the form *p, where p is an expression whose value is a NULL pointer.

CONSTRUCTORS
This class has the standard public default and copy constructors.

DESTRUCTORS
The destructor is virtual and public.

ASSIGNMENT OPERATOR
The assignment operator is public. The object is unchanged.

VIRTUAL MEMBER FUNCTIONS
virtual const char* what() const throw();
Returns a character string describing the exception type. Unless overridden in a derived class, this string is std::bad_typeid or derived class.

class std::__non_rtti

Class for reporting missing RTTI type information.

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

DESCRIPTION
This class is a SAS/C extension. An exception with this type is generated when dynamic_cast or typeid is applied to a object with polymorphic type constructed in a compilation unit that was compiled without the RTTI option.

CONSTRUCTORS
This class has the standard public default and copy constructors.

DESTRUCTORS
The destructor is virtual and public.

ASSIGNMENT OPERATOR
The assignment operator is public. The object is unchanged.

VIRTUAL MEMBER FUNCTIONS
virtual const char* what() const throw();
Returns a character string describing the exception type. Unless overridden in a derived class, this string is std::__non_rtti or derived class.


typeinfo.h Header File

The typeinfo.h header file is provided for compatibilty with older C++ code. It includes the header and makes the class names type_info, bad_cast, bad_typeid, and _ _non_rtti available in the global scope as using declarations. It also makes the names terminate_handler, set_terminate, and terminate available in the global scope by using declarations.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.