enable_error_info.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. //This example shows how to throw exception objects that support
  5. //transporting of arbitrary data to the catch site, even for types
  6. //that do not derive from boost::exception.
  7. #include <boost/exception/all.hpp>
  8. #include <stdexcept>
  9. typedef boost::error_info<struct tag_std_range_min,size_t> std_range_min;
  10. typedef boost::error_info<struct tag_std_range_max,size_t> std_range_max;
  11. typedef boost::error_info<struct tag_std_range_index,size_t> std_range_index;
  12. template <class T>
  13. class
  14. my_container
  15. {
  16. public:
  17. size_t size() const;
  18. T const &
  19. operator[]( size_t i ) const
  20. {
  21. if( i > size() )
  22. throw boost::enable_error_info(std::range_error("Index out of range")) <<
  23. std_range_min(0) <<
  24. std_range_max(size()) <<
  25. std_range_index(i);
  26. //....
  27. }
  28. };