Mega Search
23.2 Million


Sign Up

Make a donation  
MSVC++ anonymous union in struct -- compile-time initializat  
News Group: comp.lang.c++

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.

Thank you in advance for any assistance.

Best regards,
Rick C. Hodgin

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 6:24 AM EST
From: Rick C. Hodgin
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
On 8/29/2014 1:26 PM, Victor Bazarov wrote:
> On 8/29/2014 4:15 PM, Paavo Helde wrote:
>> "Rick C. Hodgin"  wrote in
>> news:37e780ad-f774-41be-a1ac-8d6ae52d2dd1@googlegroups.com:
>>
>>> On Friday, August 29, 2014 2:15:50 PM UTC-4, Ben Bacarisse wrote:
>>>> "Rick C. Hodgin"  writes:
>>>> On C++ missing C's designated initialisers...
>>>>
>>>>> Does anyone know why the C++ designers prohibited this type of
>>>>> compile-time initialization at any point?  It rather significantly
>>>>> hobbles the general use of unions, doesn't it?  Limits their full
>>>>> functionality solely to run-time code.
>>>>
>>>> No, because there is a C++ way to do this: supply an overloaded
>>>> constructor.  It's both a more and a less general solution, but C++'s
>>>> initialisation is vastly different to C's so that's to be expected.
>>>
>>>
>>> Do constructors work like that (for data populated into static blocks
>>> at compile-time)?  I always thought constructors were for data
>>> requested, and therefore code executed, at run-time.
>>>
>>> What would be the syntax for my example?
>>
>> struct SExample
>> {
>>     SExample(int i): x(0), i(i) {}
>>     SExample(float f): x(1), f(f) {}
>>     SExample(double d): x(2), d(d) {}
>>      int     x;
>>      union {
>>          int    i;
>>          float  f;
>>          double d;
>>      };
>> };
>>
>> SExample gList[] = {1, 1.0f, 2.0};
>>
>> This is not compile-time though, but rather dynamic initialization of
>> static variables. So some code probably runs, but this happens before
>> main() is called.
>
> Also, isn't it true that SExample is thus no longer a POD?  I mean,
> formally, of course, but with all that it entails...
>
> V

I think if you add trivial copy, move  and default constructors, it
becomes a POD again.  At least as of C++11.   DISCLAIMER: IANALL,
nor do I play one on TV.


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 2:26 PM EST
From: Scott Neugroschl
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
"Rick C. Hodgin"  writes:

> On Friday, August 29, 2014 2:15:50 PM UTC-4, Ben Bacarisse wrote:
>> "Rick C. Hodgin"  writes:
>> On C++ missing C's designated initialisers...
>>
>> > Does anyone know why the C++ designers prohibited this type of compile-time
>> > initialization at any point?  It rather significantly hobbles the general
>> > use of unions, doesn't it?  Limits their full functionality solely to
>> > run-time code.
>> 
>> No, because there is a C++ way to do this: supply an overloaded
>> constructor.  It's both a more and a less general solution, but C++'s
>> initialisation is vastly different to C's so that's to be expected.
>
> Do constructors work like that (for data populated into static blocks
> at compile-time)?  I always thought constructors were for data requested,
> and therefore code executed, at run-time.
>
> What would be the syntax for my example?

You don't need any special syntax (I assume you know the syntax for a
constructor!) so if you've tried it, it's likely that you don't have a
new enough compiler.  You need, I think, C++ 2011 ("extended initializer
lists").


-- 
Ben.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 9:40 PM EST
From: Ben Bacarisse
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
On 8/29/2014 4:15 PM, Paavo Helde wrote:
> "Rick C. Hodgin"  wrote in
> news:37e780ad-f774-41be-a1ac-8d6ae52d2dd1@googlegroups.com:
>
>> On Friday, August 29, 2014 2:15:50 PM UTC-4, Ben Bacarisse wrote:
>>> "Rick C. Hodgin"  writes:
>>> On C++ missing C's designated initialisers...
>>>
>>>> Does anyone know why the C++ designers prohibited this type of
>>>> compile-time initialization at any point?  It rather significantly
>>>> hobbles the general use of unions, doesn't it?  Limits their full
>>>> functionality solely to run-time code.
>>>
>>> No, because there is a C++ way to do this: supply an overloaded
>>> constructor.  It's both a more and a less general solution, but C++'s
>>> initialisation is vastly different to C's so that's to be expected.
>>
>>
>> Do constructors work like that (for data populated into static blocks
>> at compile-time)?  I always thought constructors were for data
>> requested, and therefore code executed, at run-time.
>>
>> What would be the syntax for my example?
>
> struct SExample
> {
> 	SExample(int i): x(0), i(i) {}
> 	SExample(float f): x(1), f(f) {}
> 	SExample(double d): x(2), d(d) {}
>      int     x;
>      union {
>          int    i;
>          float  f;
>          double d;
>      };
> };
>
> SExample gList[] = {1, 1.0f, 2.0};
>
> This is not compile-time though, but rather dynamic initialization of
> static variables. So some code probably runs, but this happens before
> main() is called.

Also, isn't it true that SExample is thus no longer a POD?  I mean, 
formally, of course, but with all that it entails...

V
-- 
I do not respond to top-posted replies, please don't ask

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 4:26 PM EST
From: Victor Bazarov
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
"Rick C. Hodgin"  wrote in
news:37e780ad-f774-41be-a1ac-8d6ae52d2dd1@googlegroups.com: 

> On Friday, August 29, 2014 2:15:50 PM UTC-4, Ben Bacarisse wrote:
>> "Rick C. Hodgin"  writes:
>> On C++ missing C's designated initialisers...
>>
>> > Does anyone know why the C++ designers prohibited this type of
>> > compile-time initialization at any point?  It rather significantly
>> > hobbles the general use of unions, doesn't it?  Limits their full
>> > functionality solely to run-time code.
>> 
>> No, because there is a C++ way to do this: supply an overloaded
>> constructor.  It's both a more and a less general solution, but C++'s
>> initialisation is vastly different to C's so that's to be expected.
> 
> 
> Do constructors work like that (for data populated into static blocks
> at compile-time)?  I always thought constructors were for data
> requested, and therefore code executed, at run-time.
> 
> What would be the syntax for my example?

struct SExample
{
	SExample(int i): x(0), i(i) {}
	SExample(float f): x(1), f(f) {}
	SExample(double d): x(2), d(d) {}
    int     x;
    union {
        int    i;
        float  f;
        double d;
    };
};

SExample gList[] = {1, 1.0f, 2.0};

This is not compile-time though, but rather dynamic initialization of 
static variables. So some code probably runs, but this happens before 
main() is called.

HTH
Paavo

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 3:15 PM EST
From: Paavo Helde
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
On Friday, August 29, 2014 2:15:50 PM UTC-4, Ben Bacarisse wrote:
> "Rick C. Hodgin"  writes:
> On C++ missing C's designated initialisers...
>
> > Does anyone know why the C++ designers prohibited this type of compile-time
> > initialization at any point?  It rather significantly hobbles the general
> > use of unions, doesn't it?  Limits their full functionality solely to
> > run-time code.
> 
> No, because there is a C++ way to do this: supply an overloaded
> constructor.  It's both a more and a less general solution, but C++'s
> initialisation is vastly different to C's so that's to be expected.


Do constructors work like that (for data populated into static blocks
at compile-time)?  I always thought constructors were for data requested,
and therefore code executed, at run-time.

What would be the syntax for my example?

> -- 
> Ben.

Best regards,
Rick C. Hodgin

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 12:49 PM EST
From: Rick C. Hodgin
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
"Rick C. Hodgin"  writes:

On C++ missing C's designated initialisers...

> Does anyone know why the C++ designers prohibited this type of compile-time
> initialization at any point?  It rather significantly hobbles the general
> use of unions, doesn't it?  Limits their full functionality solely to
> run-time code.

No, because there is a C++ way to do this: supply an overloaded
constructor.  It's both a more and a less general solution, but C++'s
initialisation is vastly different to C's so that's to be expected.

> Wait... Wait just a moment... Is that why they did it?  Was this a political
> movement?  A battle?  A rift among the ones with the pen and those actually
> down in the trenches? :-)  Well my my.

I doubt it's anything so nefarious (what's to be gained?).  C++
initialisation is very different to C initialisation, so maybe the
committee simply could fit, or could not do the work needed to fit,
designated initialisers into the C++ scheme of things.


-- 
Ben.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 7:15 PM EST
From: Ben Bacarisse
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
On Friday, August 29, 2014 11:19:49 AM UTC-4, Victor Bazarov wrote:
> On 8/29/2014 10:53 AM, Rick C. Hodgin wrote:
> > Does anyone know why the C++ designers prohibited this type of compile-time
> > initialization at any point?  [..snip..]
> 
> This perhaps is better asked in comp.std.c++, the newsgroup where the 
> standardization process is usually discussed.


Gracias.

Best regards,
Rick C. Hodgin

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 8:44 AM EST
From: Rick C. Hodgin
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
On 8/29/2014 10:53 AM, Rick C. Hodgin wrote:
> On Friday, August 29, 2014 9:39:08 AM UTC-4, Victor Bazarov wrote:
>> The Standard doesn't allow that, AFAIK.  See 8.5.1/15.
>>  [..]
>
> Does anyone know why the C++ designers prohibited this type of compile-time
> initialization at any point?  [..snip..]

This perhaps is better asked in comp.std.c++, the newsgroup where the 
standardization process is usually discussed.

V
-- 
I do not respond to top-posted replies, please don't ask

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 11:19 AM EST
From: Victor Bazarov
 
Re: MSVC++ anonymous union in struct -- compile-time initial  
News Group: comp.lang.c++
On Friday, August 29, 2014 9:39:08 AM UTC-4, Victor Bazarov wrote:
> 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


Does anyone know why the C++ designers prohibited this type of compile-time
initialization at any point?  It rather significantly hobbles the general
use of unions, doesn't it?  Limits their full functionality solely to
run-time code.

Wait... Wait just a moment... Is that why they did it?  Was this a political
movement?  A battle?  A rift among the ones with the pen and those actually
down in the trenches? :-)  Well my my.

"Alex, I'll take 'Preplexing Standards Decision' for $1,000, please."
"Why it's the Daily Double!  How much will you wager, Mr. Coder?"
"I'll wager ... $3500, Alex."
"The brace-enclosed compile-time initialization of these were hobbled
severely in C++ without dipping into implementation-specific extensions."
"What are unions?"
"Correct!  And with that you now move into first place.  Unless, of course,
you actually need the general use of compile-time initialization in unions,
in which case you move to last place."
"Yes.  Sad, isn't it, Alex?"
"It sure is, Mr. Coder.  Pathetic actually."
"I agree."
"And so do I."
"Thank you."
"And you as well."
"Oohh... these are fun times I must say."
"We'll cherish them forever."
"Indeed we will."

[pause]

"What were we doing?"
"The game show?"
"Oh yes... choose the next category."
"I'd like 'Things You Can Do With Onions' for $200."

Best regards,
Rick C. Hodgin

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 29-Aug-2014, at 7:53 AM EST
From: Rick C. Hodgin