fork download
  1. #include <tuple>
  2. #include <utility>
  3.  
  4. #include <boost/preprocessor.hpp>
  5.  
  6. #define FOREACH_PREFIX BOOST_PP_CAT(__foreach_, __LINE__)
  7.  
  8. #define FOREACH_BODY BOOST_PP_CAT(FOREACH_PREFIX, _body__)
  9. #define FOREACH_BREAK BOOST_PP_CAT(FOREACH_PREFIX, _break__)
  10. #define FOREACH_CONTINUE BOOST_PP_CAT(FOREACH_PREFIX, _continue__)
  11. #define FOREACH_ELEM BOOST_PP_CAT(FOREACH_PREFIX, _elem__)
  12. #define FOREACH_ONCE BOOST_PP_CAT(FOREACH_PREFIX, _once__)
  13.  
  14. #define foreachpair(KEY, VALUE, ELEMS) \
  15.   for (auto&& FOREACH_ELEM : ELEMS) \
  16.   if (false) FOREACH_BREAK: break; /* set up the break path */ \
  17.   else if (bool FOREACH_CONTINUE = false) {} /* var decl */ \
  18.   else if (true) goto FOREACH_BODY; /* skip the loop exit checks */ \
  19.   else for (;;) /* determine whether we should break or continue. */ \
  20.   if (!FOREACH_CONTINUE) goto FOREACH_BREAK; /* break */ \
  21.   else if (true) break; /* continue */ \
  22.   else \
  23.   FOREACH_BODY: \
  24.   if (bool FOREACH_ONCE = false) {} /* var decl */ \
  25.   else for (KEY = std::get<0>( \
  26.   std::forward<decltype(FOREACH_ELEM)>(FOREACH_ELEM)); \
  27.   !FOREACH_ONCE; \
  28.   FOREACH_ONCE = true) \
  29.   for (VALUE = std::get<1>( \
  30.   std::forward<decltype(FOREACH_ELEM)>(FOREACH_ELEM)); \
  31.   !FOREACH_CONTINUE; \
  32.   FOREACH_CONTINUE = true)
  33.  
  34. /* -------------------------------------------------------------------------- */
  35.  
  36. #include <iostream>
  37. #include <map>
  38. #include <vector>
  39.  
  40. int main() {
  41. {
  42. std::cout << "{}: start" << std::endl;
  43. std::map<int, double> elems;
  44. foreachpair (int key, double value, elems) {
  45. std::cout << key << " " << value << std::endl;
  46. }
  47. std::cout << "{}: end" << std::endl;
  48. }
  49. {
  50. std::cout << "{{101, 202}, {303, 404}}: start" << std::endl;
  51. std::map<int, int> elems = {{101, 202}, {303, 404}};
  52. foreachpair (int key, int value, elems) {
  53. std::cout << key << " " << value << std::endl;
  54. }
  55. std::cout << "{{101, 202}, {303, 404}}: end" << std::endl;
  56. }
  57. {
  58. std::cout << "break: {{1, 2}, {3, 4}}: start" << std::endl;
  59. std::map<int, int> elems = {{1, 2}, {3, 4}};
  60. foreachpair (int key, int value, elems) {
  61. std::cout << key << " " << value << std::endl;
  62. if (key == 1) break;
  63. }
  64. std::cout << "break: {{1, 2}, {3, 4}}: end" << std::endl;
  65. }
  66. {
  67. std::cout << "continue: {{1, 2}, {3, 4}}: start" << std::endl;
  68. std::map<int, int> elems = {{1, 2}, {3, 4}};
  69. foreachpair (int key, int value, elems) {
  70. if (key == 1) continue;
  71. std::cout << key << " " << value << std::endl;
  72. }
  73. std::cout << "continue: {{1, 2}, {3, 4}}: end" << std::endl;
  74. }
  75. {
  76. std::cout << "{{1, 2}, {3, 4}}: start" << std::endl;
  77. std::vector<std::tuple<int, int>> elems = {std::make_tuple(1, 2),
  78. std::make_tuple(3, 4)};
  79. foreachpair (int key, int value, elems) {
  80. std::cout << key << " " << value << std::endl;
  81. }
  82. std::cout << "{{1, 2}, {3, 4}}: end" << std::endl;
  83. }
  84. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
{}: start
{}: end
{{101, 202}, {303, 404}}: start
101 202
303 404
{{101, 202}, {303, 404}}: end
break: {{1, 2}, {3, 4}}: start
1 2
break: {{1, 2}, {3, 4}}: end
continue: {{1, 2}, {3, 4}}: start
3 4
continue: {{1, 2}, {3, 4}}: end
{{1, 2}, {3, 4}}: start
1 2
3 4
{{1, 2}, {3, 4}}: end