dozniak wrote:
~ wrote:
But an union would need to contain all possible C types for it to be a truly raw variant data type.
Yes, and with a limitation that if you initialise the union with
Code:
u.type = INT;
u.intVal = 5;
you cannot portably do later
Code:
myDoubleVar = u.doubleVal;
Same with std::variant in c++ - if you put an int in, you cannot take a double out.
It's good to know. With this, I think that now it's clear that the portability of C is really limited. But we could make a list of cases where we state that a way of doing things is portable to which architectures. That would probably the best if we are to handle such low level details. At least I think that if it works in an x86 CPU, it will work in any other of the same kind or probably all of them, and if so, we can use the same code, and if it's a detail that works the same in any CPU, then we can point out how portable it is.
Solar wrote:
Only the conversion to / from an array of unsigned char is well-defined.
That means, you might manage to get a C implementation going for a specific platform / compiler, but nothing portable.
And you still haven't answered the core question:
What for?
To have a variant type that can be used as any data type solely by using type casts so that it allows an unrestricted low level access of the byte data for variables for easy manipulation and display, like assembly but without having to use it.