473,791 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple union member initialization

struct Foo {
union {
int& i;
float& j;
};
Foo(int& val) :
i((int&)val), j((float&)val)
{}
};

GCC will fail to compile the above code because multiple union member is
being initialized.
But if I modify the code to just only init the reference i:
Foo(int& val) :
i((int&)val), j((float&)val)
{}
it also pop out error because reference j is not been initialized.

What's the solution to the above problem? How the C++ standard say about
using reference in union?

--
exmat - C++ matrix library
http://exmat.sourceforge.net
~ Samba, more than a low cost File and Printer server ~

-- Let us OpenSource --
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
5 5231
Ricky Lung wrote:

struct Foo {
union {
int& i;
float& j;
};
Foo(int& val) :
i((int&)val), j((float&)val)
{}
};

GCC will fail to compile the above code because multiple union member is
being initialized.
But if I modify the code to just only init the reference i:
Foo(int& val) :
i((int&)val), j((float&)val)
{}
it also pop out error because reference j is not been initialized.

What's the solution to the above problem? How the C++ standard say about
using reference in union?


I haven't looked it up right now, but if I remember correctly, then
at most *one* member of a union can have an initialization. Since
your union consists of 2 references, and references have to be initialzed ....
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
Yes, and it's a contradiction.

--
exmat - C++ matrix library
http://exmat.sourceforge.net
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:41******** *******@gascad. at...
Ricky Lung wrote:

struct Foo {
union {
int& i;
float& j;
};
Foo(int& val) :
i((int&)val), j((float&)val)
{}
};

GCC will fail to compile the above code because multiple union member is
being initialized.
But if I modify the code to just only init the reference i:
Foo(int& val) :
i((int&)val), j((float&)val)
{}
it also pop out error because reference j is not been initialized.

What's the solution to the above problem? How the C++ standard say about
using reference in union?
I haven't looked it up right now, but if I remember correctly, then
at most *one* member of a union can have an initialization. Since
your union consists of 2 references, and references have to be initialzed

.....

--
Karl Heinz Buchegger
kb******@gascad .at

~ Samba, more than a low cost File and Printer server ~

-- Let us OpenSource --
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #3
In message <41******@usene t.hk>, Ricky Lung <ri***@y-trace.com> writes
struct Foo {
union {
int& i;
float& j;
};
Foo(int& val) :
i((int&)val), j((float&)val)
All other considerations aside, how could you initialise a reference to
float using an int? What would you expect to happen?
{}
};

GCC will fail to compile the above code because multiple union member is
being initialized.
But if I modify the code to just only init the reference i:
Foo(int& val) :
i((int&)val), j((float&)val)
{}
it also pop out error because reference j is not been initialized.

What's the solution to the above problem? How the C++ standard say about
using reference in union?


What's the problem you're trying to solve by using a union?

--
Richard Herring
Jul 22 '05 #4
The long stor is, I am make a matrix class with SIMD enabled.
Some specific funtion I needed to use Intel SSE intrinsic function like
_mm_add_ps.
Therefore I want to keep the intrinsic type and a C-array in a union
something like
union {
__m128;
float[4];
};

But problem comes out when I use reference (because of some reason, I have
to use reference).
Since the cost of one reference is 4 bytes, by using union of reference, the
size of my object (reference to vector of 4 flaot) will be just 4 bytes
insteal of 8 bytes. That the reason I use union of reference

--
exmat - C++ matrix library
http://exmat.sourceforge.net

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:E9******** ******@baesyste ms.com...
In message <41******@usene t.hk>, Ricky Lung <ri***@y-trace.com> writes
struct Foo {
union {
int& i;
float& j;
};
Foo(int& val) :
i((int&)val), j((float&)val)


All other considerations aside, how could you initialise a reference to
float using an int? What would you expect to happen?
{}
};

GCC will fail to compile the above code because multiple union member is
being initialized.
But if I modify the code to just only init the reference i:
Foo(int& val) :
i((int&)val), j((float&)val)
{}
it also pop out error because reference j is not been initialized.

What's the solution to the above problem? How the C++ standard say about
using reference in union?


What's the problem you're trying to solve by using a union?

--
Richard Herring

~ Samba, more than a low cost File and Printer server ~

-- Let us OpenSource --
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #5
In message <41********@use net.hk>, Ricky Lung <ri***@y-trace.com> writes

[please don't top-post]
--
[please don't put quoted material after your sig-separator - any
reasonable news client truncates it, making sensibly-quoted followups
difficult]
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:E9******* *******@baesyst ems.com...
In message <41******@usene t.hk>, Ricky Lung <ri***@y-trace.com> writes
>struct Foo {
> union {
> int& i;
> float& j;
> };
> Foo(int& val) :
> i((int&)val), j((float&)val)
All other considerations aside, how could you initialise a reference to
float using an int? What would you expect to happen?
I still don't understand what you think you're doing here.
> {}
>};
>
>GCC will fail to compile the above code because multiple union member is
>being initialized.
>But if I modify the code to just only init the reference i:
>Foo(int& val) :
> i((int&)val), j((float&)val)
> {}
>it also pop out error because reference j is not been initialized.
>
>What's the solution to the above problem? How the C++ standard say about
>using reference in union?


What's the problem you're trying to solve by using a union?

The long stor is, I am make a matrix class with SIMD enabled. Some
specific funtion I needed to use Intel SSE intrinsic function like
_mm_add_ps.


So we're off-topic for standard C++. Nevertheless...
Therefore I want to keep the intrinsic type and a C-array in a union
something like
union {
__m128;
float[4];
};
Fair enough, though you could equally use reinterpret_cas t.
But problem comes out when I use reference (because of some reason, I
have to use reference). Since the cost of one reference is 4 bytes, by
using union of reference, the size of my object (reference to vector of
flaot) will be just 4 bytes insteal of 8 bytes.
Plus the 8 bytes somewhere else that it references. Reference members
don't save space, they just indirect. Under the hood, at the assembler
level it's just implemented as a pointer.
That the reason I use union of reference

You're using the union to alias two data items of different types. The
references are indirections (in effect pointers) so you end up aliasing
the pointers, not the data. I suspect that isn't what you intend.

--
Richard Herring
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8954
by: alexwu | last post by:
typedef union _MYUNION struct { short a; short b; }; long c; } MYUNION;
5
8150
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
3335
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under gcc 3.2.2 but does not produce the expected results, the expected results being the ones annotated in the comments in the code): #include <stdlib.h>
18
2381
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and compiler), and it is, what I conclude from the below code union data_type {
30
3284
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
4
13838
by: Theo R. | last post by:
Hi all, I have the following struct defined - #define INTEGER 0 #define STRING 1 typedef struct { char type ; union {
3
2554
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
4
10543
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1 = 100; unionMember.union1.field2 = 200 }; fullStructType var2 = { { unionMember.union2 = 300 } , { unionMember.union2 = 400 } };
7
6422
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.