#include <iostream>
#include <utility>
#include <vector>
template <typename Container>
class reverse_adaptor
{
public: // Construction
reverse_adaptor(Container &container) :
m_container(container)
{}
public: // STL container static polymorphism
auto begin() const -> decltype(std::declval<Container&>().rbegin())
{
return m_container.rbegin();
}
auto end() const -> decltype(std::declval<Container&>().rend())
{
return m_container.rend();
}
private: // Members
Container &m_container;
};
template <typename Container>
reverse_adaptor<Container> make_reverse_adaptor(Container &container)
{
return reverse_adaptor<Container>(container);
}
int main()
{
std::vector<int> test = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (const int i : make_reverse_adaptor(test))
{
std::cout << "i = " << i << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPHZlY3Rvcj4KCnRlbXBsYXRlIDx0eXBlbmFtZSBDb250YWluZXI+CmNsYXNzIHJldmVyc2VfYWRhcHRvcgp7CnB1YmxpYzogLy8gQ29uc3RydWN0aW9uCiAgICByZXZlcnNlX2FkYXB0b3IoQ29udGFpbmVyICZjb250YWluZXIpIDoKICAgICAgICBtX2NvbnRhaW5lcihjb250YWluZXIpCiAgICB7fQoKcHVibGljOiAvLyBTVEwgY29udGFpbmVyIHN0YXRpYyBwb2x5bW9ycGhpc20KICAgIGF1dG8gYmVnaW4oKSBjb25zdCAtPiBkZWNsdHlwZShzdGQ6OmRlY2x2YWw8Q29udGFpbmVyJj4oKS5yYmVnaW4oKSkKICAgIHsKICAgICAgICByZXR1cm4gbV9jb250YWluZXIucmJlZ2luKCk7CiAgICB9CgogICAgYXV0byBlbmQoKSBjb25zdCAtPiBkZWNsdHlwZShzdGQ6OmRlY2x2YWw8Q29udGFpbmVyJj4oKS5yZW5kKCkpCiAgICB7CiAgICAgICAgcmV0dXJuIG1fY29udGFpbmVyLnJlbmQoKTsKICAgIH0KCnByaXZhdGU6IC8vIE1lbWJlcnMKICAgIENvbnRhaW5lciAmbV9jb250YWluZXI7Cn07Cgp0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