0

In particular I am trying to create some wrapper functions around boost::dynamic_bitset. There is a similar question, Overloading subscript operator for non-array elements although I'm hoping for a more compact solution without creating additional classes. The relevant snippet:

typdef boost::dynamic_bitset<> BitWorld;
class Game{
    struct Grid{
        int size;
        BitWorld world;
    } grid;
public:
    BitWorld::reference& operator()(int x, int y) { return grid.world[x+y*grid.size]; }
    BitWorld::reference& operator[](int index) { return grid.world[index]; }
}

With MSVC13_64 this doesn't give me any errors, although it produces very strange behaviour when put to the test, with MinGW492_32 I get errors like this:

C:\repositories\..\logic\game_prim.h:78: error: invalid initialization of non-const reference of type 'boost::dynamic_bitset<>::reference&' from an rvalue of type 'boost::dynamic_bitset<>::reference' BitWorld::reference& operator[](int index) { return grid.world[index]; } ^

I don't see why they made working with mutating operations on proxy references so non-trivial. Simple casting won't work either, const_cast cannot convert between rvalue and lvalue refs and I didn't achieve much success with static_cast either.

Community
  • 1
  • 1
MatrixAndrew
  • 265
  • 3
  • 13
  • They I'd be trying to assign to rvalue references, or wait.. – MatrixAndrew Sep 16 '15 at 08:44
  • Yes it works flawlessly, as well as with std::vector::reference. I had the wrong idea of what a proxy reference is, it really is a reference; and the other answer confused me a bit. Thanks. – MatrixAndrew Sep 16 '15 at 09:00

0 Answers0