Tuesday, September 7, 2010

Comma Operator

After the following statement:
    int i = (10, 5);
The value of i is 5.

The comma operator is being used above, so the last value, 5, is the value of the expression (10, 5).

Reference: The C++ Standard Library by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 20.

Monday, September 6, 2010

Exceptions

The following Exception Classes are related as follows:
A logic_error is a domain_error. A runtime_error is a range_error.

Reference: The C++ Standard Library by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 26.

A way to remember this:
It is a logic_error for providing bad input (domain) to an object.
During an object's runtime, something might go out of range.
(It's just a mnemonic)

Sunday, September 5, 2010

Exception Specifications

A function's exception specification of throw() indicates the function does not throw exceptions. If the function does throw an exception, the program will terminate unless some extra measures are taken. Exception specifications have fallen out of favor because of this; although the Standard C++ Library sometimes uses them to indicate their exception guarantees.

Saturday, September 4, 2010

Exceptions

The following include files declare the Standard C++ Library's exceptions: <exception>, <new>, <typeinfo>, <ios>, and <stdexcept>.

Reference: The C++ Standard Library by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 28.

Friday, September 3, 2010

std::exception

throw std::exception("my bad"); does not compile, but throw std::out_of_range("too big"); does.

std::exception can be derived from, but cannot be thrown directly. It is the base class for all of the C++ Standard Library exceptions. Here is the exception hierarchy:
exception
        bad_alloc
        bad_cast
        bad_exception
        bad_typeid
        io_base::failure
        logic_error
            domain_error
            invalid_argument
            length_error
            out_of_range
        runtime_error
            overflow_error
            range_error
            underflow_error
Reference: The C++ Standard Library by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 30.

Thursday, September 2, 2010

Local Object Destructors

When you call exit(), the destructors of local objects do not get called before the program ends.

Wednesday, September 1, 2010

auto_ptr

It is not okay to pass auto_ptrs as references.

Reference: The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 43.

See also the following link for some reasons why:
http://nicoletti.50megs.com/cpp_notes/auto_ptr/pass.html