DEC C++
Using DEC C++ for DIGITAL UNIX Systems


Previous Contents Index

6.6.4 Numeric Limits Class Data Types

enum float_round_style

This data type is used to indicate the rounding mode for floating point arithmetic. It contains the following values:
Value Rounding Style
round_indeterminate Indeterminable
round_toward_zero Toward zero
round_to_nearest To the nearest representable value
round_toward_infinity Toward infinity
round_neg_infinity Toward negative infinity

6.7 The auto_ptr Class

The auto_ptr class provides a simple class for smart pointers; it stores a pointer to an object obtained by way of the new operator and deletes that object when the auto_ptr object is destroyed. The auto_ptr class provides semantics of strict ownership. After construction an auto_ptr object owns the object whose pointer it holds. When an instantiation of auto_ptr is copied, ownership of the object is transferred to the destination auto_ptr. The behavior is undefined if more than one instantiation of auto_ptr owns the same object at the same time.

The ANSI C++ September 1996 auto_ptr class makes use of member templates for its copy constructor and assignment operator. Because the current DEC C++ compiler does not support member templates, auto_ptr does not make use of them for copy construction or assignment operations.

6.7.1 The auto_ptr Member Functions

explicit auto_ptr(T* p=0) throw();

Constructs an object of class auto_ptr<T>. The pointer held by the auto_ptr class is initialized to p. The p variable points to an object of type T or a class derived from T for which delete p is defined and accessible, or p is a null pointer.

auto_ptr(const auto_ptr<T>& a) throw();

Constructs an object of class auto_ptr<T>. The argument a is copied to *this. If a owns the held pointer, ownership is transferred to *this.

auto_ptr<T>& operator=(const auto_ptr<T>& rhs) throw();

The argument rhs is copied to *this. If rhs owns the held pointer, ownership is transferred to *this. If *this already owns a pointer (not equal to the pointer owned by rhs), that held pointer is deleted first.

~auto_ptr();

If *this owns the held pointer, the pointer is deleted.

T& operator*() const throw();

Returns a reference to the object to which the underlying held pointer points.

T* operator->() const throw();

Returns the underlying held pointer.

T* get() const throw();

Returns the underlying held pointer.

T* release() throw();

Returns the underlying held pointer after releasing ownership of it.

6.8 The Standard Exception Library

The standard exception classes are all classes derived from the exception class and are used in the standard library to report errors detected during program execution. A simple example of using this class follows:


 
#include <stdexcept> 
#include <string> 
 
int main() { 
        try { 
                string s("abc"); 
                if (s.at(4)=='d') ; // out of range 
        } 
        catch(const exception& e) { 
                cout << e.what() << endl; 
        } 
        catch(...) { 
                cout << "unknown exception" << endl; 
        } 
} 
 

The output from this example is as follows:


position beyond end of string 

Users may also use the standard exception hierarchy to report errors in their own classes.

6.8.1 Types of Standard Exceptions

All standard exceptions are rooted at the base class exception. No standard library errors throw an object of type exception (they are always of a more specific or derived type). This class is simply used as a base class when a user wants to catch all standard exceptions, as in the previous example.

Six classes are derived from exception:

The logic_error class has four derived classes:

The runtime_error class has three derived classes:

6.8.2 The exception Member Functions

exception() throw();

Constructs an object of class exception.

exception(const exception&) throw();

Copies an exception object.

exception& operator=(const exception&) throw();

Assigns an exception object.

virtual ~exception() throw();

Destroys the exception object.

virtual const char* what() const throw();

Returns a null-terminated byte string that contains information about the type of error. The contents of the string are implementation dependent. In the DEC C++ library the string contains an indication of the problem---that is, string index out of range or invalid string size parameter.

6.8.3 Derived Exception Classes

All of the standard exception classes are derived from class exception; thus, they inherit all of the exception class member functions. The only difference is in the constructor---all have an overridden constructor that takes a string argument; that is:


class logic_error: public exception { 
public: 
 logic_error(const string& what_arg); 
}; 
 
class domain_error: public logic_error { 
public: 
 domain_error(const string& what_arg); 
}; 
 
class invalid_argument: public logic_error { 
public: 
 invalid_argument(const string& what_arg); 
}; 
 
class length_error: public logic_error { 
public: 
 length_error(const string& what_arg); 
}; 
 
class out_of_range: public logic_error { 
public: 
 out_of_range(const string& what_arg); 
}; 
 
class runtime_error: public exception { 
public: 
 runtime_error(const string& what_arg); 
}; 
 
class range_error: public runtime_error { 
public: 
 range_error(const string& what_arg); 
}; 
 
class overflow_error: public runtime_error { 
public: 
 overflow_error(const string& what_arg); 
}; 
 
class underflow_error: public runtime_error { 
public: 
 underflow_error(const string& what_arg); 
}; 

6.9 The Complex Math Library

A complex number has a real part and an imaginary part. The standard complex library utilizes templates, so that any underlying type can be used to represent these parts. Template specializations are provided for float, double, and long double.

6.9.1 Example of Use

A simple example of use is:


#include <complex> 
 
int main() { 
 complex<double> c1(1,1), c2(3.14,3.14); 
 cout << "c2/c1: " << c2/c1 << endl; 
} 

Note that you must explicitly link in the math library when using the complex library on DIGITAL UNIX, that is:


cxx prog.cxx -lm 

6.9.2 Complex Math Member Functions

complex(const T& re = T(), const T& im = T());

Constructs an object of type complex with the real part set to re and the imaginary part set to im.

complex(const complex<T>& c);

Constructs a complex number by copying all elements from c.

complex<T>& operator=(const complex<T>& c);

Assigns c to *this and returns *this.

complex<T>& operator=(const T& c);

Assigns c to the real part of *this. Sets the imaginary part of *this to zero and returns *this.

complex<T>& operator+=(const complex<T>& c);

Adds c to *this and then returns the result.

complex<T>& operator-=(const complex<T>& c);

Subtracts c from *this and then returns the result.

complex<T>& operator*=(const complex<T>& c);

Multiplies c and *this and then returns the result.

complex<T>& operator/=(const complex<T>& c);

Divides *this by c and then returns the result.

T real();

Returns the real part of *this.

T imag();

Returns the imaginary part of *this.

6.9.3 Complex Math Nonmember Functions

template <class T>

T real(const complex<T>& c);

Returns the real part of c.

template <class T>

T imag(const complex<T>& c);

Returns the imaginary part of c.

template <class T>

T abs(const complex<T>& c);

Returns the magnitude of c.

template <class T>

T norm(const complex<T>& c);

Returns the squared magnitude of c.

template <class T>

T arg(const complex<T>& c);

Returns the phase angle of c.

template <class T>

complex<T> conj(const complex<T>& c);

Returns the complex conjugate of c.

template <class T>

complex<T> polar(const T& rho, const T& theta = 0);

Returns the complex value corresponding to a complex number whose magnitude is rho and whose phase angle is theta.

template <class T>

complex<T> cos(const complex<T>& c);

Returns the complex cosine of c.

template <class T>

complex<T> cosh(const complex<T>& c);

Returns the complex hyperbolic cosine of c.

template <class T>

complex<T> exp(const complex<T>& c);

Returns the complex base e exponential of c.

template <class T>

complex<T> log(const complex<T>& c);

Returns the complex natural (base e) logarithm of x.

template <class T>

complex<T> log10(const complex<T>& c);

Returns the complex common (base 10) logarithm of x.

template <class T>
complex<T> pow(const complex<T>& x, int y); template <class T> complex<T> pow(const complex<T>& x, int y); template <class T> complex<T> pow(const complex<T>& x, const complex<T>& y);

template <class T>
complex<T> pow(const complex<T>& x, const T& y); template <class T> complex<T> pow(const T& x, const complex<T>& y);

Returns the complex power of base x raised to the y-th power.

template <class T>

complex<T> sin(const complex<T>& c);

Returns the complex sine of c.

template <class T>

complex<T> sinh(const complex<T>& c);

Returns the complex hyperbolic sine of c.

template <class T>

complex<T> sqrt(const complex<T>& c);

Returns the complex square root of c, in the range of the right half-plane.

template <class T>

complex<T> tan(const complex<T>& c);

Returns the complex tangent of c.

template <class T>

complex<T> tanh(const complex<T>& c);

Returns the complex hyperbolic tangent of c.

6.9.4 Complex Math Nonmember Operators

template <class T>
complex<T> operator+(const complex<T>& lhs, const T& rhs);

template <class T>
complex<T> operator+(const T& lhs, const complex<T>& rhs);

Adds rhs to lhs and returns the sum.

template <class T>
complex<T> operator+(const complex<T>& lhs, const T& rhs);

template <class T>
complex<T> operator+(const T& lhs, const complex<T>& rhs);

Subtracts rhs from lhs and returns the difference.

template <class T>
complex<T> operator*(const complex<T>& lhs, const T& rhs);

template <class T>
complex<T> operator*(const T& lhs, const complex<T>& rhs);

Multiplies rhs and lhs and returns the product.

template <class T>
complex<T> operator/(const complex<T>& lhs, const T& rhs);

template <class T>
complex<T> operator/(const T& lhs, const complex<T>& rhs);

Divides rhs into lhs and returns the quotient.

template <class T>
complex<T> operator:=,=(const complex<T>& lhs, const T& rhs);

template <class T>
complex<T> operator:=,=(const T& lhs, const complex<T>& rhs);

Compares lhs to rhs and returns true if they are equal, otherwise returns false. The imaginary parts of the scalars are assumed to be the value T().

template <class T>
complex<T> operator!=(const complex<T>& lhs, const T& rhs);

template <class T>
complex<T> operator!=(const T& lhs, const complex<T>& rhs);

Compares lhs to rhs and returns true if they are not equal, otherwise returns false. The imaginary parts of the scalars are assumed to be T().

template <class T, class charT, class traits>

istream& operator>>(istream& is, complex<T>& c);

Extracts a complex number c from the istream is of the form u, (u), or (u,v), where u is the real part and v is the imaginary part. If bad input is encountered, sets ios::failbit.

template <class T, class charT, class traits>

ostream operator<<(ostream& os, const complex<T>& c);

Returns os << "(" << c.real() << "," << c.imag() << ")".

6.9.5 Typedefs

typedef T value_type;

The type used to represent the real and imaginary parts of the complex number.

6.9.6 Unsafe Downcasts

Because the DEC C++ compiler does not yet support the explicit keyword (that is, nonconverting constructors), unsafe downcasts will be allowed; that is, users can convert a complex<double> to a complex<float>.

6.9.7 Upgrading from the Nonstandard Complex Math Library

This section explains how to upgrade from the pre-ANSI DEC C++ complex library to the current DEC C++ standard complex library, which is based on the 24 September 1996 ANSI C++ working draft.

In the ANSI library, complex objects are templatized on the type of the real and imaginary parts, the pre-ANSI DEC C++ library, complex objects are not templatized. The pre-ANSI library assumes the type is double, whereas the new library provides specializations for float, double, and long double as well as allowing users to specialize on their own floating point types.

Also note that mathematical error checking is not supported in the ANSI library. Users who rely on detection of underflow, overflow, and divide by zero should continue using the pre-ANSI DEC C++ complex library.

The following is a detailed list of important changes:

6.10 The Allocator Class

The Standard C++ Library defines an allocator class that is used to control the memory model containers will use. Every container class in the Standard C++ library takes an allocator as one of its template arguments. In this way each container can manage its own storage requirements. Containers can make use of different memory models by using allocators that implement different types of memory management schemes. A default allocator that makes use of the global new and delete is provided by the Standard library.

The allocator as defined in the 24 September 1996 draft C++ Standard makes use of member function templates and member class templates. The lack of member template support in the current version of DEC C++ mandates the use of an allocator that does not utilize this feature. Therefore the DEC C++ library provides an alternate allocator class. The alternate allocator allocates raw bytes of storage. Its types and functions are provided by an allocator_interface class. Under this scheme the allocator makes use of the allocator_interface class to allocate typed storage rather than allocating itself directly. The implementation is not standard conforming so its use will be documented in this section.

Note that the current version of the DEC C++ library also contains a specialization for void. The specialization for void does not conform to standard either. Refer to the 24 September 1996 ANSI C++ draft for details on the ANSI C++ draft conforming allocator.

The rest of this section defines the allocator class provided with the current version of DEC C++. It also describes how to build custom allocators and containers that make use of this allocator class.


Previous Next Contents Index