C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

union of references?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
bruceleegrip
Guest





PostPosted: Thu Sep 23, 2004 8:08 am    Post subject: union of references? Reply with quote



Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

When trying to compile this I get an error that I must initialize all
the references in the initializer list of the constructor. When I add
that the compiler gives me a warning saying that i'm initializing a
union member that's already been initialized. I can change the
references to pointers to work around it but I was wondering if
there's a way to do it using references.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bob Hairgrove
Guest





PostPosted: Fri Sep 24, 2004 11:41 am    Post subject: Re: union of references? Reply with quote



On 23 Sep 2004 04:08:27 -0400, [email]rforte (AT) gmail (DOT) com[/email] (bruceleegrip) wrote:

Quote:
Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

When trying to compile this I get an error that I must initialize all
the references in the initializer list of the constructor. When I add
that the compiler gives me a warning saying that i'm initializing a
union member that's already been initialized. I can change the
references to pointers to work around it but I was wondering if
there's a way to do it using references.

Not according to the current standard. The last sentence of paragraph
9.5.1 says it all: "If a union contains a static data member, or a
member of reference type, the program is ill-formed."

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Fri Sep 24, 2004 2:11 pm    Post subject: Re: union of references? Reply with quote



In article <59d497b9.0409221335.505e6208 (AT) posting (DOT) google.com>,
bruceleegrip <rforte (AT) gmail (DOT) com> writes
Quote:
Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

When trying to compile this I get an error that I must initialize all
the references in the initializer list of the constructor. When I add
that the compiler gives me a warning saying that i'm initializing a
union member that's already been initialized. I can change the
references to pointers to work around it but I was wondering if
there's a way to do it using references.

How can a single object be used to simultaneously designate three other
objects but only use one designator because that is what you are asking
for. Now consider your alternative formulation using pointers:

union {
const std::string * ref1;
const std::string * ref2;
const std::string * ref3;
};

At any one time that (anonymous) union can only point to a single
instance of std::string, whichever one was last assigned to it. You
actually gain nothing other than obfuscation by writing such code.

This is generally true any time the members of a union have the same
type. Unions are for allowing a block of raw memory to be reused for
objects with different types. IOWs unions have nothing to offer for the
code you are writing, just declare three reference members in class Foo.



--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
news
Guest





PostPosted: Fri Sep 24, 2004 3:28 pm    Post subject: Re: union of references? Reply with quote


"bruceleegrip" <rforte (AT) gmail (DOT) com> wrote

Quote:
Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

The above is ill-formed. You can not have references in unions. See the last sentence of

the first paragraph of 9.5 of the C++standard. Static members and references are NOT allowed.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ben Hutchings
Guest





PostPosted: Fri Sep 24, 2004 3:41 pm    Post subject: Re: union of references? Reply with quote

bruceleegrip wrote:
Quote:
Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

When trying to compile this I get an error that I must initialize all
the references in the initializer list of the constructor.

This is correct; all references must be initialised.

Quote:
When I add that the compiler gives me a warning saying that i'm
initializing a union member that's already been initialized.

This is also correct; only the first member of a union can be
initialised. Although the use of multiple reference members in unions
doesn't seem to be explicitly ruled out by the standard, it appears
to be implicit in these two rules.

Quote:
I can change the references to pointers to work around it but I was
wondering if there's a way to do it using references.

I think you will have to use pointers (or find an alternative to the
union).

--
Ben Hutchings
Who are all these weirdos? - David Bowie, about L-Space IRC channel #afp

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Sat Sep 25, 2004 5:36 am    Post subject: Re: union of references? Reply with quote

On 23 Sep 2004 04:08:27 -0400, [email]rforte (AT) gmail (DOT) com[/email] (bruceleegrip) wrote
in comp.lang.c++.moderated:

Quote:
Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

When trying to compile this I get an error that I must initialize all
the references in the initializer list of the constructor. When I add
that the compiler gives me a warning saying that i'm initializing a
union member that's already been initialized. I can change the
references to pointers to work around it but I was wondering if
there's a way to do it using references.

No, there isn't. A union has many members, but only one at a time.
It is not possible to initialize any member of a union other than the
first. While you have a value in the first member, the other members
do not exist.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gerhard Menzl
Guest





PostPosted: Sat Sep 25, 2004 5:39 am    Post subject: Re: union of references? Reply with quote

bruceleegrip wrote:

Quote:
Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

When trying to compile this I get an error that I must initialize all
the references in the initializer list of the constructor. When I add
that the compiler gives me a warning saying that i'm initializing a
union member that's already been initialized. I can change the
references to pointers to work around it but I was wondering if
there's a way to do it using references.

"If a union contains a static data member, or a member of reference
type, the program is ill-formed." (9.5/1)

What do you want to achieve? Your references are all of the same type;
if you simply want a Foo object to refer to different strings over its
lifetime, why don't you simply use a single pointer?

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Sat Sep 25, 2004 5:34 pm    Post subject: Re: union of references? Reply with quote

[email]rforte (AT) gmail (DOT) com[/email] (bruceleegrip) wrote in message
news:<59d497b9.0409221335.505e6208 (AT) posting (DOT) google.com>...

Quote:
Is it possible to have a class that does the following:
class Foo
{
union
{
const std::string & ref1;
const std::string & ref2;
const std::string & ref3;
};
...
Foo( const std::string & r ) : ref1(r) { }
};

No. §9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed."

I take this to mean that you should get a compiler error for the
definition of the union.

Quote:
When trying to compile this I get an error that I must initialize all
the references in the initializer list of the constructor. When I add
that the compiler gives me a warning saying that i'm initializing a
union member that's already been initialized. I can change the
references to pointers to work around it but I was wondering if
there's a way to do it using references.

A union cannot contain a reference. Period. I don't know why the
compiler isn't complaining earlier, when the union is defined. (This
isn't a rule invented by the committee, either. I think it has always
been the case.)

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Mon Sep 27, 2004 9:18 pm    Post subject: Re: union of references? Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in message
Quote:
...
No. §9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed."

I take this to mean that you should get a compiler error for the
definition of the union.
...
A union cannot contain a reference. Period. I don't know why the
compiler isn't complaining earlier, when the union is defined. (This
isn't a rule invented by the committee, either. I think it has always
been the case.)


Against the
Quote:
§9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed",
ARM(The Annotated C++ Reference Manual) only say "A union can have no

static member" at §9.5 and say nothing about a member of reference
type. Edison Design Group does not refer to the change from ARM to the
standard at http://www.edg.com/cpp_ftrs.html.

As a matter of fact, my MSVC++6 allows the following code, as well as
MSVC++7.1.

How about gc++? I wander what is the intention of the change.

#include <iostream>
using namespace std;

union U {
U(int& n1, int& n2): nref1(n1), nref2(n2) {}
int& nref1;
int& nref2;
};


int main(int argc, char* argv[])
{
int n1 = 1;
int n2 = 2;
U u(n1, n2);// u.nref1 == u.nref2 == n2

u.nref1 = 3;
cout << n1 << endl; // "1" is printed out
cout << n2 << endl; // "3" is printed out

return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Mon Sep 27, 2004 9:22 pm    Post subject: Re: union of references? Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in message
Quote:
...
No. §9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed."

I take this to mean that you should get a compiler error for the
definition of the union.
...
A union cannot contain a reference. Period. I don't know why the
compiler isn't complaining earlier, when the union is defined. (This
isn't a rule invented by the committee, either. I think it has always
been the case.)


Against the
Quote:
§9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed",
ARM(The Annotated C++ Reference Manual) only say "A union can have no

static member" at §9.5 and say nothing about a member of reference
type. Edison Design Group does not refer to the change from ARM to the
standard at http://www.edg.com/cpp_ftrs.html.

As a matter of fact, my MSVC++6 allows the following code, as well as
MSVC++7.1.

How about gc++? I wander what is the intention of the change.

#include <iostream>
using namespace std;

union U {
U(int& n1, int& n2): nref1(n1), nref2(n2) {}
int& nref1;
int& nref2;
};


int main(int argc, char* argv[])
{
int n1 = 1;
int n2 = 2;
U u(n1, n2);// u.nref1 == u.nref2 == n2

u.nref1 = 3;
cout << n1 << endl; // "1" is printed out
cout << n2 << endl; // "3" is printed out

return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
news
Guest





PostPosted: Tue Sep 28, 2004 10:21 am    Post subject: Re: union of references? Reply with quote


"Tokyo Tomy" <hosoda (AT) jtec (DOT) or.jp> wrote

the
Quote:
§9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed",
ARM(The Annotated C++ Reference Manual) only say "A union can have no
static member" at §9.5 and say nothing about a member of reference
type. Edison Design Group does not refer to the change from ARM to the
standard at http://www.edg.com/cpp_ftrs.html.

The ARM is an outdated document that has no bearing on either the C++ standard

or the EDG compiler. If you read the first paragraph of the web page you quote
above you will see that it specifically says that 3.4 of their front end uses the standard.
The list of post-ARM additions is just a list of "new features" it's not a definitive list
of the difference between their compiler and the ARM.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Sep 28, 2004 9:23 pm    Post subject: Re: union of references? Reply with quote

[email]hosoda (AT) jtec (DOT) or.jp[/email] (Tokyo Tomy) wrote in message
news:<49c1da0b.0409270822.1ef40d17 (AT) posting (DOT) google.com>...
Quote:
kanze (AT) gabi-soft (DOT) fr wrote in message
...
No. §9.5/1, last sentence: "If a union contains a static data
member, or a member of reference type, the program is ill-formed."

I take this to mean that you should get a compiler error for the
definition of the union.
...
A union cannot contain a reference. Period. I don't know why the
compiler isn't complaining earlier, when the union is defined.
(This isn't a rule invented by the committee, either. I think it
has always been the case.)

Against the
§9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed",
ARM(The Annotated C++ Reference Manual) only say "A union can have no
static member" at §9.5 and say nothing about a member of reference
type. Edison Design Group does not refer to the change from ARM to the
standard at http://www.edg.com/cpp_ftrs.html.

I suspect that the absense of a mention of references in the ARM is just
an oversight, and that the intent was always that they be banned from
unions.

It's hard to see what a reference in a union could possibly mean.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Wed Sep 29, 2004 5:53 pm    Post subject: Re: union of references? Reply with quote

"news" <news (AT) news (DOT) newshosting.com> wrote

Quote:
"Tokyo Tomy" <hosoda (AT) jtec (DOT) or.jp> wrote

the
§9.5/1, last sentence: "If a union contains a static data member,
or a member of reference type, the program is ill-formed",
ARM(The Annotated C++ Reference Manual) only say "A union can have no
static member" at §9.5 and say nothing about a member of reference
type. Edison Design Group does not refer to the change from ARM to the
standard at http://www.edg.com/cpp_ftrs.html.

The ARM is an outdated document that has no bearing on either the C++ standard
or the EDG compiler. If you read the first paragraph of the web page you quote
above you will see that it specifically says that 3.4 of their front end uses the standard.
The list of post-ARM additions is just a list of "new features" it's not a definitive list
of the difference between their compiler and the ARM.

Thank you for your comment, News.

The ARM was a de facto standard at that time, on which many compilers
were implemented and many codes were written. It would have been easy
for the standard to be borne from a desert. I don't think the standard
has no bearing from the ARM. Please notice the fact that the EDG
document refer to the ARM shows how important it was.

I understand that the change in union is a trivial change and is not
counted as the "new features" in the EDG document, but I also wander
why the trivial change was made in the standard, though it is trivial,
which brakes some existing codes at least.

I suspected in my previous post that the change was made arbitrarily
without proper reasons. Dose the reference in union cause any
inconvenient problems? I expect counter opinions against my prejudice.
Thank you in advance.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Thu Sep 30, 2004 10:47 am    Post subject: Re: union of references? Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in message news:<d6652001.0409280056.7ffd147d (AT) posting (DOT) google.com>...
Quote:
...
I suspect that the absense of a mention of references in the ARM is just
an oversight, and that the intent was always that they be banned from
unions.

It's hard to see what a reference in a union could possibly mean.

No oversight on my side. If the reference had been banned in union,
the right place to mention it should have been at the place following
the sentence "A union can have no static member."

I don't know about oversight on the side of the authors, Bjarne
Stroustrup et al. But, I believe no oversight on the authors' side. As
an evidence, I would like to say that my MSVC++6 follows the ARM
practice in this respect.

The union in C++ is treated as C++ struct with many limitations. It
was easy for compilers to pose the limitation on reference on them in
the process of giving the other limitations to them. However, it is
not easy for compliers to do so now, because of many existing users.

If the reference in union does not cause any subtle inconveniences,
the standard should remain as the ARM was, nevertheless it was a
oversight by the authors or not, because it is easy for the compilers,
which already have the van on the reference in union, to relax the
limitations.

I wander why Kanze think that
Quote:
the intent was always that they be banned from unions.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Fri Oct 01, 2004 3:36 pm    Post subject: Re: union of references? Reply with quote

In article <49c1da0b.0409282346.17e8efc2 (AT) posting (DOT) google.com>, Tokyo Tomy
<hosoda (AT) jtec (DOT) or.jp> writes
Quote:
If the reference in union does not cause any subtle inconveniences,
the standard should remain as the ARM was, nevertheless it was a
oversight by the authors or not, because it is easy for the compilers,
which already have the van on the reference in union, to relax the
limitations.

Inconveniences? IMO there is a fundamental mismatch between the concept
of a C++ reference and the concept of a union. A C++ reference provides
a name (possibly empty in the case of a return by reference) for an
existing object. The name is immutably bound to the object. Unions are
to allow alternative objects to occupy the same storage space. IOWs
unions are about memory reuse and objects, references are about names
for existing objects.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.