c++vectorreference-wrapper

reference_wrapper cause "incomplete type is not allowed"


#include <vector>
#include <array>

int main() {
    typedef std::array<int,2> point;
    typedef std::vector<std::reference_wrapper<point>> route;

    std::vector<std::vector<point>> lattice = {
        {point{ {0,0} },point{ {0,1} }},
        {point{ {1,0} },point{ {1,1} }}
    };

    route r = {&lattice[0][0],&lattice[0][1]};

    point last = r.back().get();
}

The last line won't compile because "incomplete type is not allowed". When I search on internet, a paper seems to address this problem: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0357r3.html. That paper is just above my level. So I am not sure. Would anyone give a confirmation or a solution to this problem?

Also, workarounds are appreciated:

I am trying to store routes on a not-so-large lattice. There will be a lot of routes (dense in the lattice). So, I am thinking, within a route, store reference of points instead of points in order to save memory.


Solution

  • You must #include <functional> before using std::reference_wrapper.