 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
dragoncoder Guest
|
Posted: Sat Jul 23, 2005 10:04 am Post subject: vector of references |
|
|
Hello all,
I heard STL containers use copy semantics i.e they keep a copy of the
original object.
If thats the case, what is the meaning of a vector declaration
parameterized on references? How is a vector of this sort initialized,
as references need to be initialized when they are defined.
In short my question is "What does such a vector contain ?".
/P
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Sat Jul 23, 2005 1:43 pm Post subject: Re: vector of references |
|
|
On Sat, 23 Jul 2005 14:04:15 +0400, dragoncoder <pktiwary (AT) gmail (DOT) com> wrote:
| Quote: | I heard STL containers use copy semantics i.e they keep a copy of the
original object.
If thats the case, what is the meaning of a vector declaration
parameterized on references? How is a vector of this sort initialized,
as references need to be initialized when they are defined.
|
References have wrong assignment sematics for vector. They are only
initialized as pointers, but behave as objects.
| Quote: | In short my question is "What does such a vector contain ?".
|
You can't use vector of references in practice, so the question makes
little sense.
--
Maxim Yegorushkin
<firstname.lastname (AT) gmail (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Sat Jul 23, 2005 1:44 pm Post subject: Re: vector of references |
|
|
"dragoncoder" <pktiwary (AT) gmail (DOT) com> wrote
| Quote: | Hello all,
I heard STL containers use copy semantics i.e they keep a copy of the
original object.
If thats the case, what is the meaning of a vector declaration
parameterized on references? How is a vector of this sort initialized,
as references need to be initialized when they are defined.
In short my question is "What does such a vector contain ?".
|
You cannot have a vector of references, as references cannot be
re-assigned. Once you intialized the reference, it will always reference
the same object. So references do not meet the requirements for a type
that a vector can hold.
regards
--
jb
(reply address in rot13, unscramble first)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Sun Jul 24, 2005 11:24 am Post subject: Re: vector of references |
|
|
dragoncoder wrote:
| Quote: | I heard STL containers use copy semantics
|
True.
| Quote: | If thats the case, what is the meaning of a vector declaration
parameterized on references? How is a vector of this sort initialized,
as references need to be initialized when they are defined.
In short my question is "What does such a vector contain ?".
|
If you try to compile the following very short program, I believe that
all your questions will be answered:
#include <vector>
int main()
{
std::vector<int&> refVec;
}
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sun Jul 24, 2005 11:30 am Post subject: Re: vector of references |
|
|
dragoncoder wrote:
| Quote: | Hello all,
I heard STL containers use copy semantics i.e they keep a copy of the
original object.
If thats the case, what is the meaning of a vector declaration
parameterized on references?
|
Try it.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Jul 24, 2005 11:40 am Post subject: Re: vector of references |
|
|
dragoncoder wrote:
| Quote: | I heard STL containers use copy semantics i.e they keep a copy
of the original object.
If thats the case, what is the meaning of a vector declaration
parameterized on references?
|
Undefined behavior. References do not meet the requirements for
the instantiation type of std::vector.
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre 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 |
|
 |
Thomas Tutone Guest
|
Posted: Sun Jul 24, 2005 11:35 pm Post subject: Re: vector of references |
|
|
James Kanze wrote:
| Quote: | dragoncoder wrote:
I heard STL containers use copy semantics i.e they keep a copy
of the original object.
If thats the case, what is the meaning of a vector declaration
parameterized on references?
Undefined behavior. References do not meet the requirements for
the instantiation type of std::vector.
|
I would go even further. Although technically a vector of references
may result in undefined behavior, I don't think that a
standard-conforming compiler and library could even compile a program
purporting to include such a constuct, because it would involve a
reference to a reference. At any rate, I tried it on three compilers,
and all choked.
Best regards,
Tom
[ 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
|
Posted: Mon Jul 25, 2005 3:19 pm Post subject: Re: vector of references |
|
|
Thomas Tutone wrote:
| Quote: | James Kanze wrote:
dragoncoder wrote:
I heard STL containers use copy semantics i.e they keep a
copy of the original object.
If thats the case, what is the meaning of a vector
declaration parameterized on references?
Undefined behavior. References do not meet the requirements
for the instantiation type of std::vector.
I would go even further. Although technically a vector of
references may result in undefined behavior, I don't think
that a standard-conforming compiler and library could even
compile a program purporting to include such a constuct,
because it would involve a reference to a reference. At any
rate, I tried it on three compilers, and all choked.
|
The standard says it's undefined behavior. So an implementation
is free to make it work, or make it do anything else. By means
of partial specialization, other meta-programming techiques, or
even compiler magic. So while I suspect that in practice, you
normally will get a compiler error, formally, it's still
undefined behavior.
--
James Kanze GABI Software
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 |
|
 |
|
|
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
|
|