fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. struct rgba
  7. {
  8. uint8_t r, g, b, a;
  9.  
  10. constexpr uint8_t& operator[](size_t x)
  11. {
  12. return const_cast<uint8_t*>(&r)[x];
  13. }
  14. };
  15.  
  16. struct rgba2
  17. {
  18. uint8_t r, g, b, a;
  19.  
  20. constexpr uint8_t& operator[](size_t x)
  21. {
  22. assert(x>=0 && x<4);
  23. switch (x){
  24. case 0: return r;
  25. case 1: return g;
  26. case 2: return b;
  27. case 3: return a;
  28. //default: throw(1); // not possible with constexpr
  29. }
  30. }
  31. };
  32.  
  33. struct rgba3
  34. {
  35. enum ergb { red, green, blue, alpha};
  36. uint8_t c[alpha+1];
  37.  
  38. constexpr uint8_t& operator[](ergb x)
  39. {
  40. assert(x>=0 && x<4);
  41. return c[x];
  42. }
  43. };
  44.  
  45.  
  46. int main() {
  47. // your code goes here
  48. rgba2 cola;
  49. rgba3 colb;
  50. cola[1]=2;
  51. colb[rgba3::green]=3;
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
Standard output is empty