0

Consider the program

#include<iostream>

struct A {
};

struct B {
    void f() { std::cout << "Hello world!\n"; };
};

static_assert(alignof(A) == alignof(B));

int main() {
    A a;
    reinterpret_cast<B*>(&a)->f();
};

Note that the value of a is never read or modified in the sense of the aliasing rule, as far as I can tell.

I am pretty sure that the above has undefined behavior, because I am trying to call f on an object of wrong type. But what passage in the standard actually says that?

walnut
  • 21,629
  • 4
  • 23
  • 59
  • The standard quoted in [this answer](https://stackoverflow.com/a/37119041/514235) should be answering your question. `N3337 [basic.lval]/10: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined` – iammilind Dec 15 '19 at 07:06
  • 2
    @iammilind The aliasing rule quoted in the answer is actually not answering the question, because I am explicitly not accessing any value of the object, but the comment under the answer regarding [class.mfct.non-static]/2 is what I was looking for. – walnut Dec 15 '19 at 07:08
  • I'd say the questions listed above are unrelated, but AFAIR similar question has been asked, maybe not even once. – Language Lawyer Dec 16 '19 at 19:18

0 Answers0