0

I get tired of writing a pair of iterators and make a View class.

I don't get why the make_range function doesn't work unless I remove the View(View<C>& r) constructor.

But that doesn't make sense. The make_range function doesn't use that constructor.

The code is also at https://godbolt.org/g/75sWPf

Thanks.

Test:

#include <vector>

template<typename C>
class View {
public:
    typedef typename C::iterator iterator;

    View(C& c, iterator begin, iterator end) :
            _c(c) {
        _begin = c.begin();
        _end = c.end();
    }

    // Remove a constructor
    // View(View<C>& r) : _c(r._c) {
    //     _begin = r._begin;
    //     _end = r._end;
    // }

private:
    C& _c;
    iterator _begin;
    iterator _end;
};

template<typename T, typename A>
View<std::vector<T, A> > make_view(std::vector<T, A>& v) {
    View<std::vector<T, A> > a(v, v.begin(), v.end());
    return a;
};

int main() {
    std::vector<int> a;
    for (int i = 0; i < 5; ++i) a.push_back(i);
    View<std::vector<int>> b(a, a.begin(), a.end());
    // this works.
    // View<std::vector<int>> c(b);  

    // doesn't work unless I remove constructor.
    auto d = make_view(a);  
}
R zu
  • 2,034
  • 12
  • 30
  • 3
    I would change the signature to accept `vector const&` because as the message says, you cannot pass a temporary by non-const& – Cory Kramer Apr 12 '18 at 17:33
  • Even a bad book or tutorial should have told you how to create a [copy-constructor](http://en.cppreference.com/w/cpp/language/copy_constructor) (which I assume you wanted to make). – Some programmer dude Apr 12 '18 at 17:35
  • I also assume by `make_range` you really mean `make_view`? – Some programmer dude Apr 12 '18 at 17:36
  • 2
    This is an issue where copy elision is not guaranteed. If you compile under C++17 you don't need to remove the constructor. – AndyG Apr 12 '18 at 17:37
  • 1
    As a side note, I don't know how you intend to use this `View` but it is easy to cause bad behavior. For example if you say `a.push_back()` after making a `View` of `a`, you've possibly invalidated the iterators that the `View` is holding – Cory Kramer Apr 12 '18 at 17:37
  • 2
    On an unrelated note, your `make_view` shouldn't use `std::vector` explicitly, it should use a single template argument just like the `View` class does. Then you could use *any* container or container-like type. – Some programmer dude Apr 12 '18 at 17:38
  • @Some programmer dude let me try that. I haven't read book/taken any cs class, and just search google as I write code ... – R zu Apr 12 '18 at 17:42
  • 1
    That's not really a good way to learn. I strongly suggest you [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Apr 12 '18 at 17:45
  • Oh, and once you've learned everything you can from your views and ranges, scrap them and use [a good library](https://github.com/ericniebler/range-v3) instead. :) – Some programmer dude Apr 12 '18 at 17:46

0 Answers0