You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

range_for.hpp 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_RANGE_FOR_HPP
  14. #define RANGES_V3_RANGE_FOR_HPP
  15. #include <range/v3/range_fwd.hpp>
  16. #include <range/v3/range/access.hpp>
  17. #if RANGES_CXX_RANGE_BASED_FOR < RANGES_CXX_RANGE_BASED_FOR_17
  18. /// A range-based for macro, basically a hack until the built-in range-for can handle
  19. /// Ranges that have a different type for begin and end. \ingroup range-core
  20. #define RANGES_FOR(VAR_DECL, ...) \
  21. if(bool CPP_PP_CAT(_range_v3_done, __LINE__) = false) {} \
  22. else \
  23. for(auto && CPP_PP_CAT(_range_v3_rng, __LINE__) = (__VA_ARGS__); \
  24. !CPP_PP_CAT(_range_v3_done, __LINE__);) \
  25. for(auto CPP_PP_CAT(_range_v3_begin, __LINE__) = \
  26. ranges::begin(CPP_PP_CAT(_range_v3_rng, __LINE__)); \
  27. !CPP_PP_CAT(_range_v3_done, __LINE__); \
  28. CPP_PP_CAT(_range_v3_done, __LINE__) = true) \
  29. for(auto CPP_PP_CAT(_range_v3_end, __LINE__) = \
  30. ranges::end(CPP_PP_CAT(_range_v3_rng, __LINE__)); \
  31. !CPP_PP_CAT(_range_v3_done, __LINE__) && \
  32. CPP_PP_CAT(_range_v3_begin, __LINE__) != \
  33. CPP_PP_CAT(_range_v3_end, __LINE__); \
  34. ++CPP_PP_CAT(_range_v3_begin, __LINE__)) \
  35. if(!(CPP_PP_CAT(_range_v3_done, __LINE__) = true)) {} \
  36. else \
  37. for(VAR_DECL = *CPP_PP_CAT(_range_v3_begin, __LINE__); \
  38. CPP_PP_CAT(_range_v3_done, __LINE__); \
  39. CPP_PP_CAT(_range_v3_done, __LINE__) = false) \
  40. /**/
  41. #else
  42. #define RANGES_FOR(VAR_DECL, ...) for(VAR_DECL : (__VA_ARGS__))
  43. #endif
  44. #endif