一个很好支持的替代c ++联合? [英] A well supported alternative to c++ unions?

查看:118
本文介绍了一个很好支持的替代c ++联合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为工会可以完美的我的想法,特别是当我认为我的代码应该运行在一个真正的异构家庭的机器,特别是低功率的机器,什么是bug我是事实,谁创建编译器似乎不太在意介绍和提供良好的工会支持,例如这个表格实际上是空的,当涉及到无限制联盟支持,这是一个真正令人不快的视图为我的项目。

I think that unions can be perfect for what i have in mind and especially when I consider that my code should run on a really heterogeneous family of machines, especially low-powered machine, what is bugging me is the fact that the people who creates compilers doesn't seem to care too much about introducing and offering a good union support, for example this table is practical empty when it comes to Unrestricted Unions support, and this is a real unpleasant view for my projects.

union 可以替代至少模仿相同的属性?

There are alternatives to union that can at least mimic the same properties ?

推荐答案

p> Union在大多数编译器上得到很好的支持,不支持的是包含具有非重要构造函数(无限制联合)的成员的联合。实际上,在创建联盟时,您几乎总是需要一个自定义构造函数,所以没有不受限制的联合更是一个不便之处。

Union is well supported on most compilers, what's not well supported are unions that contains members that have non trivial constructor (unrestricted unions). In practice, you'd almost always want a custom constructor when creating unions, so not having unrestricted union is more of a matter of inconvenience.

或者, void指向指向具有足够大小的malloc-ed内存的最大成员的指针。缺点是你需要显式类型转换。

Alternatively, you can always use void pointer pointing to a malloc-ed memory with sufficient size for the largest member. The drawback is you'd need explicit type casting.

这篇关于一个很好支持的替代c ++联合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆