Reinterpret this in C++: legal or not?
No, that's definitely not legal. For a number of reasons.
The first reason is, you've got *this
dereferencing an AddOne<int>*
which doesn't actually point to an AddOne<int>
. It doesn't matter that the operation doesn't really require a dereference "behind the scenes"; *foo
is only legal if foo
points to an object of compatible type.
The second reason is similar: You're calling a member function on an AddOne<int>
which isn't. It likewise doesn't matter that you don't access any of AddOne
's (nonexistent) members: the function call itself is an access of the object value, running afoul of the strict aliasing rule.