Skip to main content
deleted 16 characters in body
Source Link

The exact posted code has has defined behavior under c++11, since demo is a standard-layout class and info (the data member) is accessed as a compatible type, but it is bad style.

A standard-layout class has the same access control for all data members, no virtual functions, only inherits from standardStandard-layout class andmust:

— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and

— has no base classes of the same type as the first non-static data member.

  • Have the same access control for all data members
  • Have no virtual functions
  • Only inherit from standard-layout classes
  • At most one class in its class hierarchy has data members
  • Has no data members with the same type as any in the class hierarchy

I am quotingThis text is simplified from 9.7 of the last two clausesdraft standard. In my terminology, because I am unsure exactly what they meanclass hierarchy includes the class itself, and data members only covers non-static data members.

It would also be undefined behavior ifAccessing the data member is accessed aswith an incompatible type (forwould cause undefined behavior, as always. For instance short is undefined, but char and int is defined).

In short, such access is defined if the class is standard-layout and the data member is only accessed as a compatible type. So be VERY careful. You can check if a type is standard-layout with std::is_standard_layout

The exact posted code has has defined behavior under c++11, since demo is a standard-layout class, but it is bad style.

A standard-layout class has the same access control for all data members, no virtual functions, only inherits from standard-layout class and:

— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and

— has no base classes of the same type as the first non-static data member.

I am quoting the last two clauses, because I am unsure exactly what they mean.

It would also be undefined behavior if the data member is accessed as an incompatible type (for instance short is undefined, but char and int is defined).

In short, such access is defined if the class is standard-layout and the data member is only accessed as a compatible type. So be VERY careful. You can check if a type is standard-layout with std::is_standard_layout

The exact posted code has has defined behavior under c++11, since demo is a standard-layout class and info (the data member) is accessed as a compatible type, but it is bad style.

Standard-layout class must:

  • Have the same access control for all data members
  • Have no virtual functions
  • Only inherit from standard-layout classes
  • At most one class in its class hierarchy has data members
  • Has no data members with the same type as any in the class hierarchy

This text is simplified from 9.7 of the draft standard. In my terminology, class hierarchy includes the class itself, and data members only covers non-static data members.

Accessing the data member with an incompatible type would cause undefined behavior, as always. For instance short is undefined, but char and int is defined.

In short, such access is defined if the class is standard-layout . So be VERY careful. You can check if a type is standard-layout with std::is_standard_layout

Source Link

The exact posted code has has defined behavior under c++11, since demo is a standard-layout class, but it is bad style.

A standard-layout class has the same access control for all data members, no virtual functions, only inherits from standard-layout class and:

— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and

— has no base classes of the same type as the first non-static data member.

I am quoting the last two clauses, because I am unsure exactly what they mean.

It would also be undefined behavior if the data member is accessed as an incompatible type (for instance short is undefined, but char and int is defined).

In short, such access is defined if the class is standard-layout and the data member is only accessed as a compatible type. So be VERY careful. You can check if a type is standard-layout with std::is_standard_layout