gcc: How to use __attribute((__may_alias__)) properly to avoid “derefencing type-punned pointer” warning

后端 未结 3 1831
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 17:41

I\'ve got some code that uses type-punning to avoid having to call a member \"object\"\'s constructor and destructor unless/until it\'s actually necessary to use the object.

3条回答
  •  梦如初夏
    2021-02-06 18:12

    I would argue for having your containing class just contain a char array of sufficient size to contain your member "object" and then using placement new to initialize on top of the char array. That has the perk of being specification-compliant as well as cross-compiler. The only problem is that you have to know the size in chars of your member object, which may get you in trouble.

    Is there a reason you can't have the member be a pointer and use new and delete?

提交回复
热议问题