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.