fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct Value
  5. {
  6. int x;
  7. };
  8.  
  9. struct Delta
  10. {
  11. int dx;
  12. };
  13.  
  14. void apply(std::vector<Value> & values, const Delta & delta)
  15. {
  16. for (unsigned i = 0; i < values.size(); i++)
  17. {
  18. values[i].x += delta.dx;
  19. }
  20. }
  21.  
  22. int main()
  23. {
  24. std::vector<Value> values(2);
  25.  
  26. Delta & d = reinterpret_cast<Delta&>(values[0]);
  27. d.dx = 1;
  28.  
  29. apply(values, d);
  30.  
  31. std::cout << values[1].x << std::endl;
  32. }
  33.  
  34. // My results with GCC 4.6.3
  35. #if 0
  36. $ g++ -o test -O0 main.cpp && ./test
  37. 2
  38.  
  39. $ g++ -o test -O1 main.cpp && ./test
  40. 2
  41.  
  42. $ g++ -o test -O2 main.cpp && ./test
  43. 1
  44.  
  45. $ g++ -o test -O3 main.cpp && ./test
  46. 1
  47. #endif
Success #stdin #stdout 0.01s 2812KB
stdin
Standard input is empty
stdout
1