Does reinterpret_cast lead to undefined behavior?

前端 未结 2 1358
慢半拍i
慢半拍i 2020-12-10 05:53

I have a class template A which contains a container of pointers (T*):

template 
class A {
public:
   // ... 
pri         


        
2条回答
  •  长情又很酷
    2020-12-10 06:26

    Although the reinterpret_cast itself might be unspecified behaviour, attempting to access the parameters once you've done the cast is undefined behaviour.

    N3337 [basic.lval]/10: 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.

    Your example is none of the above.

提交回复
热议问题