C++ Logo

sg12

Advanced search

[ub] type punning through congruent base class?

From: Fabio Fracassi <f.fracassi_at_[hidden]>
Date: Mon, 6 Jan 2014 10:26:58 +0100 (CET)
Hello UB-Experts,
 
as far as I can see the following (static_cast) is UB:
 
struct B {
  int i;
};
 
struct D : B {
  void foo() { /* access B::i */ }
};
 
B b;
static_cast<D&>(b).foo();
 
because of [expr.static.cast] clause 11
 
first question: is my assessment of the situation correct or is this use legal?
if it is not (legal): could we make it legal or would we run afoul of the aliasing rules?
 
best regards
 
Fabio

Received on 2014-01-06 10:39:35