特に、boost::dynamic_bitset の周りにいくつかのラッパー関数を作成しようとしています。追加のクラスを作成することなく、よりコンパクトなソリューションを望んでいますが、非配列要素の添字演算子のオーバーロードという同様の質問があります。関連するスニペット:
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]; }
}
MSVC13_64 ではエラーは発生しませんが、テストすると非常に奇妙な動作が発生しますが、MinGW492_32 では次のようなエラーが発生します。
C:\repositories\..\logic\game_prim.h:78: エラー: タイプ 'boost::dynamic_bitset<> の右辺値からのタイプ 'boost::dynamic_bitset<>::reference&' の非 const 参照の無効な初期化::reference' BitWorld::reference& operator[](int index) { return grid.world[index]; } ^
彼らがプロキシ参照の変更操作を非常に重要なものにした理由がわかりません。単純なキャストも機能しません。const_cast は右辺値と左辺値の参照の間で変換できず、static_cast でもあまり成功しませんでした。