fork download
  1. #include <iostream>
  2.  
  3. /*
  4.  * Where in this program are we accessing the _stored value_
  5.  * of `b.y` through something other than a glvalue of type int?
  6.  * */
  7.  
  8. struct A { int x; };
  9. struct B { int y; } b { 1024 };
  10.  
  11. int main () {
  12. A * ptr = reinterpret_cast<A*> (&b); // (1), same value representation/alignment, guaranteed.
  13.  
  14. int& ref = (*ptr).x; // (2), not accessing the _stored value_ of anything,
  15. // merely forming a reference to an object
  16.  
  17. int val = ref; // (3), accessing `b.y` through a glvalue of type `int`, safe
  18. }
  19.  
  20. /*
  21.  * NOTE: 2), Just as taking the address of object X does not access the stored value of object X,
  22.  * the same thing applies when we are creating a T&; we are referring to an object, not
  23.  * the value of said object.
  24.  */
  25.  
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty