Multiple union member initialization

Discussion in 'C++' started by Ricky Lung, Aug 18, 2004.

  1. Ricky Lung

    Ricky Lung Guest

    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! =-----
    Ricky Lung, Aug 18, 2004
    #1
    1. Advertising

  2. 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
    Karl Heinz Buchegger, Aug 18, 2004
    #2
    1. Advertising

  3. Ricky Lung

    Ricky Lung Guest

    Yes, and it's a contradiction.

    --
    exmat - C++ matrix library
    http://exmat.sourceforge.net
    "Karl Heinz Buchegger" <> wrote in message
    news:...
    > 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
    >



    ~ 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! =-----
    Ricky Lung, Aug 18, 2004
    #3
  4. In message <>, Ricky Lung <> 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
    Richard Herring, Aug 18, 2004
    #4
  5. Ricky Lung

    Ricky Lung Guest

    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" <junk@[127.0.0.1]> wrote in message
    news:...
    > In message <>, Ricky Lung <> 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! =-----
    Ricky Lung, Aug 19, 2004
    #5
  6. In message <4124f6e2$>, Ricky Lung <> 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" <junk@[127.0.0.1]> wrote in message
    >news:...
    >> In message <>, Ricky Lung <> 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_cast.
    >
    >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
    Richard Herring, Aug 19, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    645
    Matt Garman
    Apr 25, 2004
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    846
    Chris Torek
    Apr 26, 2004
  3. Replies:
    3
    Views:
    823
    Peter_Julian
    Oct 10, 2005
  4. Angus
    Replies:
    1
    Views:
    2,596
  5. aaragon
    Replies:
    2
    Views:
    599
    James Kanze
    Nov 2, 2008
Loading...

Share This Page