Can Aliasing Problems be Avoided with const Variables

后端 未结 5 1370
梦谈多话
梦谈多话 2020-12-31 22:42

My company uses a messaging server which gets a message into a const char* and then casts it to the message type.

I\'ve become concerned about this afte

5条回答
  •  再見小時候
    2020-12-31 23:12

    There is no aliasing problem as you use (const)char* type, see the last point of:

    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:

    • the dynamic type of the object,
    • a cv-qualified version of the dynamic type of the object,
    • a type similar (as defined in 4.4) to 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 elements or non-static data members (including, recursively, an element or non-static data 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.

提交回复
热议问题