The <typeinfo> header declares the type_info class (for the typeid operator) and two exception classes related to type information and casting.
Exception for dynamic_cast<>
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();
};
The dynamic_cast<> operator throws bad_cast when the cast fails. See dynamic_cast in Chapter 12 for more information.
dynamic_cast keyword
Exception for null pointer in typeid expressions
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();
};
The typeid operator throws bad_typeid when it is applied to an expression of the form *p, where p is a null pointer. See typeid in Chapter 12 for more information.
typeid keyword
Type information
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);
};
The typeid operator returns a static type_info object. The type information includes the type's name and a collation order, both of which are implementation-defined. An implementation might derive classes from type_info to provide additional information.
Note that the copy constructor and assignment operators are inaccessible, so you must store pointers if you want to use a standard container. Example 13-40 shows how to store type_info pointers in a set, where the order is determined by the before function.
Example 13-40: Storing type information.
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <typeinfo>
typedef std::pointer_to_binary_function<
const std::type_info*,
const std::type_info*,
bool>
type_info_compare;
typedef std::set<const std::type_info*, type_info_compare>
typeset;
// Return true if *a comes before *b. (Comparison function
// to store type_info pointers in an associative container.)
bool type_info_less(const std::type_info* a,
const std::type_info* b)
{
return a->before(*b);
}
// Print a type_info name on a line.
void print(const std::type_info* x)
{
std::cout << x->name() << '\n';
}
void demo()
{
// Construct and initialize the set.
typeset types(std::ptr_fun(&type_info_less));
types.insert(&typeid(int));
types.insert(&typeid(char));
types.insert(&typeid(std::type_info));
types.insert(&typeid(std::bad_alloc));
types.insert(&typeid(std::exception));
...
// Print the types in the set.
std::for_each(types.begin(), types.end(), print);
}
The members of type_info are as follows:
bool
before
(const type_info& rhs) consttype_info object comes before rhs in the implementation-defined order. The relative order of types can vary between programs, even for the same types.const char*
name
() constbool
operator==
(const type_info& rhs) constbool
operator!=
(const type_info& rhs) consttype_info objects, which are equal when the types they describe are the same.typeid keyword