In the c++ standard, in [basic.lval]/11.6 says:
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:[...]
- an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),[...]
This sentence is part of the strict-aliasing rule.
Does it allow us to alias through class member access?
class A{ //"casted to" aggregate
int i;
float g;
};
int j=10;
int l = reinterpret_cast<A*>(&j)->i;
According to the standard, an object value is only accessed if the object is subject to lvalue-to-rvalue-conversion [conv.lval]/2.
[expr.ref] does not stipulate that the object-expression must refer to an object of that type, only that the glvalue must have class type (the object-expression is the expression at the left of the dot "."). Nevertheless there is a word that recurrently appears in sentence related to member access which may imply constraint on the program that I overlooked. For example [expr.ref]/4.1:
If E2 is a static data member and the type of E2 is T, then E1.E2 is an lvalue; the expression designates the named member of the class.
Could "designates" means that this member is within its lifetime and that I cannot use class member access to perfom aliasing? Or is it allowed by [basic.lval]/11.6?
&j
is pointer-interconvertible toA*
). – Rabbinicalunion
in the question. Is this a mistake? – Rabbinical*reinterpret_cast<A*>(&j)
still refer to j, this expression is a glvalue of typeA*
that refers toj
. This is the case described in [basic.lval]/11.6 – Bozeni
is anint
member,A
has standard-layout, so... – Rabbinicalj
where a subobject of an existing object of typeA
. On the other hand [basic.lval]/11.6 does not requires two objects to exist, it just about the type of the glvalue through which is performed the access to the value of an existing object. – BozenA
is aclass
, or aunion
. – Rabbinicalfp.x
could be an alias forip
no? And even iffp
were not an aggregate,fp.x
could be an alias forip
? – BozenC Rationale
into google to find open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf to receive a document that should be required reading for people intending or claiming to write quality C implementations. – Scorecard