Код: Выделить всё
#include
#include
class storage {
public:
auto get_x() { return x_ref{ *this }; }
auto get_y() { return y_ref{ *this }; }
auto& get_z() { return z; }
private:
uint8_t x : 3;
uint8_t y : 5;
uint8_t z;
struct x_ref {
storage& store;
operator uint8_t() { return store.x; }
auto& operator=(uint8_t value) { store.x = value; return *this; }
};
struct y_ref {
storage& store;
operator uint8_t() { return store.y; }
auto& operator=(uint8_t value) { store.y = value; return *this; }
};
};
int main(int argc, char **argv) {
storage s{};
s.get_x() = 3;
s.get_y() = 5;
s.get_z() = 7;
uint8_t x = s.get_x(), y = s.get_y(), z = s.get_z();
std::printf("%u, %u\n", x, y, z);
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... eneric-way