| 69 | }; |
| 70 | |
| 71 | int main() |
| 72 | { |
| 73 | std::vector<Box> boxes{ {2.0, 2.0, 3.0}, {1.0, 3.0, 2.0}, {1.0, 2.0, 1.0}, {2.0, 3.0, 3.0} }; |
| 74 | |
| 75 | // Step 1: create a view where begin() and end() are different. |
| 76 | // The result of the take_while() range adapter has this property |
| 77 | // (or, at least, it should have as per its specification in the Standard). |
| 78 | auto first_boxes{ boxes | take_while([](const Box& box) { return box.volume() < 15; }) }; |
| 79 | |
| 80 | /* This therefore generally does not compile... */ |
| 81 | //std::cout << "Volume of smallest box: " |
| 82 | // << original_find_optimum(first_boxes.begin(), first_boxes.end(), |
| 83 | // [](const Box& box1, const Box& box2) { return box1.isSmallerThan(box2); })->volume() |
| 84 | // << std::endl; |
| 85 | |
| 86 | // Side-note: you can use the std::ranges::views::common to turn a range |
| 87 | // where begin and end have a different type into a view where they do. |
| 88 | auto common_boxes{ first_boxes | common }; |
| 89 | std::cout << "Volume of smallest box: " |
| 90 | << original_find_optimum(common_boxes.begin(), common_boxes.end(), |
| 91 | [](const Box& box1, const Box& box2) { return box1.isSmallerThan(box2); })->volume() |
| 92 | << std::endl; |
| 93 | |
| 94 | // Step 2: Show off our shiny new, modernised version of find_optimum() |
| 95 | // First off: no need for the common adapter! |
| 96 | std::cout << "Volume of largest box: " |
| 97 | << find_optimum(first_boxes.begin(), first_boxes.end(), |
| 98 | [](const Box& box1, const Box& box2) { return !box1.isSmallerThan(box2); })->volume() |
| 99 | << std::endl; |
| 100 | |
| 101 | // Other cool features of our modern find_optimum() version include projection |
| 102 | // and the support for member function pointers. So no more lambdas required here! |
| 103 | |
| 104 | // Use a member-function pointer for the comparison function |
| 105 | std::cout << "Volume of smallest box: " |
| 106 | << find_optimum(first_boxes.begin(), first_boxes.end(), &Box::isSmallerThan)->volume() |
| 107 | << std::endl; |
| 108 | |
| 109 | // Use a member-function pointer for the new projection functionality |
| 110 | std::cout << "Volume of largest box: " |
| 111 | << find_optimum(first_boxes.begin(), first_boxes.end(), std::greater<>{}, &Box::volume)->volume() |
| 112 | << std::endl; |
| 113 | } |
nothing calls this directly
no test coverage detected