Billy ONeal
Billy ONeal

Reputation: 106609

Do C89 or C++03 define strict aliasing rules?

I've seen several assertions that C89 and C++03 define strict aliasing rules. I, however, cannot find that particular bit in the standard. My understanding was that strict aliasing rules were added in C99.

Upvotes: 6

Views: 562

Answers (3)

Xeo
Xeo

Reputation: 131847

The C++03 standard has the following under §3.10 [basic.lval] p15:

If a program attempts to access the stored value of an object through an lvalue of other than one of the following types the behavior is undefined48):
— the dynamic type of the object,
— a cv-qualified version of the dynamic type of the object,
— a type that is the signed or unsigned type corresponding to the dynamic type of the object,
— a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
— an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union),
— a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
— a char or unsigned char type.

Footnote:

48) The intent of this list is to specify those circumstances in which an object may or may not be aliased.

This site also lists the sections in the other standards.

Upvotes: 7

Steve Jessop
Steve Jessop

Reputation: 279385

3.3 in C89, 3.10/15 in C++03.

Both have a footnote, words to the effect of, "the intent of this list is to indicate when an object may or may not be aliased".

Upvotes: 6

caf
caf

Reputation: 239311

This text is present in C89, §3.3 EXPRESSIONS:

An object shall have its stored value accessed only by an lvalue that has one of the following types:

  • the declared type of the object,

  • a qualified version of the declared type of the object,

  • a type that is the signed or unsigned type corresponding to the
    declared type of the object,

  • a type that is the signed or unsigned type corresponding to a
    qualified version of the declared type of the object,

  • an aggregate or union type that includes one of the aforementioned
    types among its members (including, recursively, a member of a
    subaggregate or contained union), or

  • a character type.

Violation of a "shall" constraint leads to undefined behaviour, so a set of allowed aliasing rules can be derived from this text.

Upvotes: 7

Related Questions