// Example 13-38: Storing type information.
#include <algorithm>
#include <functional>
#include <iostream>
#include <ostream>
#include <set>
#include <typeinfo>

typedef bool (*type_info_compare)
  (const std::type_info*, const std::type_info*);

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