fork download
  1. #include <iostream>
  2. #include <cstdint>
  3. using namespace std;
  4.  
  5. template <typename T, unsigned int N>
  6. struct buffer {
  7. int8_t v[N];
  8.  
  9. struct proxy {
  10. int8_t *elem;
  11.  
  12. proxy(int8_t *elem) : elem(elem) {}
  13.  
  14. proxy& operator=(T value) {
  15. // whatever you need...
  16. *elem = static_cast<T>(value);
  17. return *this;
  18. }
  19. operator T() const {
  20. // whatever you need...
  21. return static_cast<T>(*elem);
  22. }
  23. };
  24.  
  25. proxy operator[](int i) {
  26. return proxy{ &v[i] };
  27. }
  28. };
  29.  
  30. int main() {
  31. buffer<double, 1> buf;
  32.  
  33. buf[0] = 2.1;
  34. std::cout << buf[0] << std::endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
2