user_defined.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // (C) Copyright Eric Niebler 2005.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. /*
  6. Revision history:
  7. 25 August 2005 : Initial version.
  8. */
  9. #include <boost/test/minimal.hpp>
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // define a user-defined collection type and teach BOOST_FOREACH how to enumerate it
  12. //
  13. namespace mine
  14. {
  15. struct dummy {};
  16. char * range_begin(mine::dummy&) {return 0;}
  17. char const * range_begin(mine::dummy const&) {return 0;}
  18. char * range_end(mine::dummy&) {return 0;}
  19. char const * range_end(mine::dummy const&) {return 0;}
  20. }
  21. #include <boost/foreach.hpp>
  22. namespace boost
  23. {
  24. template<>
  25. struct range_mutable_iterator<mine::dummy>
  26. {
  27. typedef char * type;
  28. };
  29. template<>
  30. struct range_const_iterator<mine::dummy>
  31. {
  32. typedef char const * type;
  33. };
  34. }
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // test_main
  37. //
  38. int test_main( int, char*[] )
  39. {
  40. // loop over a user-defined type (just make sure this compiles)
  41. mine::dummy d;
  42. BOOST_FOREACH( char c, d )
  43. {
  44. ((void)c); // no-op
  45. }
  46. return 0;
  47. }