Re: MSVC++ anonymous union in struct -- compile-time initialization
of non-first member
On 8/29/2014 9:24 AM, Rick C. Hodgin wrote:
Is there a compile-time workaround for this in MSVC++? Currently I
am
using GCC through MinGW in Windows to compile the area of the program
where I need this ability, and then linking them together. But, I'd
rather figure out some way to do it in a single toolset.
struct SExample
{
int x;
union {
int i;
float f;
double d;
};
};
SExample gList[] =
{
{ 0, (int)1 },
{ 1, (float)1.0f }, // Warning (1)
{ 2, (double)2.0 } // Warning (2)
};
(1) warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
(2) warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
----------
In GCC there's an extension, { 1, {.f = 1.0f} }, { 2, {.d = 2.0 } },
which I believe comes forward from C and is supported in C++ through the
GCC extensions (or possibly C11??).
Is there a compile-time workaround in MSVC++? Specifically, I'm
using
the MSVC++ tools that came with Visual Studio 2003 and Visual Studio 2008.
My MSVC++ workaround today is a run-time initialization. I
initialize them to integer values (the first union member) in source
code, and then in my custom init() code I manually populate those
entries with their proper float and double values. The actual source
code this example relates to is much longer and I have several that
need updated. I'd rather figure out a way to do it entirely at
compile time, and in the single MSVC++ toolset.
The Standard doesn't allow that, AFAIK. See 8.5.1/15.
If you need to learn about a possible extension in VC++, you will most
likely find help in their online forums, see msdn.microsoft.com or some
such.
V
--
I do not respond to top-posted replies, please don't ask