Skip to content

Instantly share code, notes, and snippets.

@jdeng
Created March 12, 2013 01:37
Show Gist options
  • Save jdeng/5139562 to your computer and use it in GitHub Desktop.
Save jdeng/5139562 to your computer and use it in GitHub Desktop.
Constructor inheritance
#include <vector>
#include <iostream>
template <typename Iterator>
struct range: public std::pair<Iterator, Iterator>
{
using Parent = std::pair<Iterator, Iterator>;
template <typename... Arg> range(Arg&& ... arg): Parent(std::forward<Arg>(arg) ...) {}
Iterator begin() { return this->first; }
Iterator end() { return this->second; }
};
template <typename Iterator>
range<Iterator> make_range(Iterator a, Iterator b) { return range<Iterator>(a, b); }
int main()
{
std::vector<int> input{1, 2, 3, 4, 5, 6};
auto range = make_range(input.cbegin(), input.cbegin() + 3);
for (auto& x: range) std::cout << x << ","; std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment