Click here to Skip to main content
15,891,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a union, which stores either a string or a double. AS you know, unions store only one type, how do I identify which type was last assigned to it, or in other words, which type is it currently storing? Plus, I'm using the D language, there isn't a tag for it, so I had to write it here.

What I have tried:

I've tried it with sizeof. I thought that each type would change the sizeof the union, but it doesn't happen, you can check the following code:
C++
tvar qa;
qa.d = 20.4;
writeln(qa.sizeof);
qa.i = 20;
writeln(qa.sizeof);
qa.s = "qa";
writeln(qa.sizeof);
readln;



tqvar is the variant here.
Posted
Updated 18-Mar-16 23:46pm
v2
Comments
Richard MacCutchan 19-Mar-16 5:46am    
The size of the union is determined by the largest element that it contains.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900