What are Unrestricted Unions proposed in C++11?

后端 未结 3 1688
天涯浪人
天涯浪人 2020-12-14 09:38

I gather unrestricted unions as one of the functionality being put forth in C++11. Can anyone please explain the semantics behind this and the advantages it pro

3条回答
  •  时光说笑
    2020-12-14 10:34

    It expands unions to allow any type, not just "plain old data", giving you more flexibility to store different types of data in the same location without resorting to manual pointer hackery.

    The price you pay for this is that you have to do some careful book keeping. With a plain old data union assignment was enough to change the "current type" and reading the wrong type was likely to result in garbled data but not much more than that. With a non plain old data union you must keep track of the current type and call the correct constructors and destructors manually to change the current type and to clean things up correctly when destroying the union as a whole. If you try to read or write the wring type bad things are likely to happen

提交回复
热议问题