fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <typeinfo>
  4. #include <memory>
  5. #include <boost/iterator/indirect_iterator.hpp>
  6. using namespace std;
  7.  
  8. struct HeavyResource{};
  9. class Owner
  10. {
  11. private:
  12. vector<unique_ptr<HeavyResource>> _vectorOfHeavyResources;
  13. public:
  14. using ResourceIterator =
  15. boost::indirect_iterator<std::vector<std::unique_ptr<HeavyResource>>::iterator,
  16. const HeavyResource>;
  17. ResourceIterator begin() { return std::begin(_vectorOfHeavyResources); }
  18. ResourceIterator end() { return std::end(_vectorOfHeavyResources); }
  19. Owner() : _vectorOfHeavyResources(5)
  20. {}
  21. };
  22.  
  23.  
  24. int main() {
  25. Owner o;
  26. for (const auto& b : o)
  27. {
  28. cout << typeid(b).name() << endl;
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
13HeavyResource
13HeavyResource
13HeavyResource
13HeavyResource
13HeavyResource