 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
C++ Guest
|
Posted: Tue Dec 13, 2005 6:10 pm Post subject: Re: Vector syntax question |
|
|
Hi:
Can anybody tell me what "std::vector<int>& m_vec = std::vector<int>();"
means, especially what the last bracket means in it.
[ 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: Tue Dec 13, 2005 11:03 pm Post subject: Re: Vector syntax question |
|
|
C++ wrote:
| Quote: | Can anybody tell me what "std::vector<int>& m_vec = std::vector<int>();"
means, especially what the last bracket means in it.
|
I'm not sure what you mean by "last bracket."
That code shouldn't compile, since it attempts to bind a non-const
reference to a temporary.
std::vector<int>() is the default constructor for a std::vector<int>,
which would be of size 0. So that part creates a temporary
std::vector<int> of size 0.
std::vector<int>& m_vec is a reference to a std::vector<int> that is
named m_vec.
So this code attempts to assign a reference to a temporary
default-constructed std::vector<int>. Which is not permitted.
Can you provide some context for your question?
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 |
|
 |
romayankin@gmail.com Guest
|
Posted: Tue Dec 13, 2005 11:21 pm Post subject: Re: Vector syntax question |
|
|
| Quote: | what the last bracket means in it.
This is call to default constructor of std::vector class |
-R.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paul Floyd Guest
|
Posted: Wed Dec 14, 2005 6:10 am Post subject: Re: Vector syntax question |
|
|
On 13 Dec 2005 13:10:07 -0500, C++ <leon800219 (AT) yahoo (DOT) com.cn> wrote:
| Quote: | Hi:
Can anybody tell me what "std::vector<int>& m_vec = std::vector<int>();"
means, especially what the last bracket means in it.
|
std::vector<int>& : a reference vector of ints
std::vector<int>() : default constructor for a vector of ints (creates a
vector of zero elements).
A bientot
Paul
--
Paul Floyd http://paulf.free.fr (for what it's worth)
Surgery: ennobled Gerald.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Dec 14, 2005 12:55 pm Post subject: Re: Vector syntax question |
|
|
Thomas Tutone wrote:
| Quote: | C++ wrote:
Can anybody tell me what "std::vector<int>& m_vec =
std::vector<int>();" means, especially what the last
bracket means in it.
I'm not sure what you mean by "last bracket."
That code shouldn't compile, since it attempts to bind a
non-const reference to a temporary.
std::vector<int>() is the default constructor for a
std::vector<int>, which would be of size 0. So that part
creates a temporary std::vector<int> of size 0.
std::vector<int>& m_vec is a reference to a std::vector<int
that is named m_vec.
So this code attempts to assign a reference to a temporary
default-constructed std::vector
|
Just a nit, but an important one in this context: there is no
assignment involved, only initialization. Assignment would be
legal, since assigning to a reference is the same thing as
assigning to whatever it refers to.
--
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 |
|
 |
Vladimir Ivashko Guest
|
Posted: Wed Dec 14, 2005 7:28 pm Post subject: Re: Vector syntax question |
|
|
First one is a type - vector specialization for int.
Second one is anonimous instance generating on-fly with default ctor for
assignmentt to m_vec.
rvalue object is destroyed immediately after assignmnet, but compiler can be
smart enough do not generate temporary instance.
"C++" <leon800219 (AT) yahoo (DOT) com.cn> wrote
| Quote: | Hi:
Can anybody tell me what "std::vector<int>& m_vec = std::vector<int>();"
means, especially what the last bracket means in it.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Wed Dec 14, 2005 8:59 pm Post subject: Re: Vector syntax question |
|
|
kanze wrote:
| Quote: | [...] That code shouldn't compile, since it attempts to bind a
non-const reference to a temporary.
std::vector<int>() is the default constructor for a
std::vector<int>, which would be of size 0. So that part
creates a temporary std::vector<int> of size 0.
std::vector<int>& m_vec is a reference to a std::vector<int
that is named m_vec.
So this code attempts to assign a reference to a temporary
default-constructed std::vector
Just a nit, but an important one in this context: there is no
assignment involved, only initialization.
|
A nit to your nit (good God, recursive nitpicking... We are
soo insane!!! )
If you read his statement again, it is clear that "assign" is
not what he intended to write (I'm betting it was one of those
cases that happens to all of us, where his brain was thinking
of something, and gave the order to type something, but meanwhile
his fingers decided to type something else) -- in that context,
confusing assignment with initialization would lead to "assign
a temporary to a reference". I think he clearly meant "bind",
as he used in the rest of his message.
Carlos
--
[ 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: Wed Dec 14, 2005 9:01 pm Post subject: Re: Vector syntax question |
|
|
kanze wrote:
| Quote: | Thomas Tutone wrote:
So this code attempts to assign a reference to a temporary
default-constructed std::vector<int>. Which is not permitted.
Just a nit, but an important one in this context: there is no
assignment involved, only initialization. Assignment would be
legal, since assigning to a reference is the same thing as
assigning to whatever it refers to.
|
Yes, my words were misleadingly imprecise. Thanks for the correction.
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 |
|
 |
mailsink@gmail.com Guest
|
Posted: Thu Dec 15, 2005 10:48 am Post subject: Re: Vector syntax question |
|
|
there are 2 interesting quirks that have not been addressed, and which
may have triggered the original question
first quirk: assigning a temporary to a reference (which is what is
going on here) keeps the temporary alive until the end of the scope
so while the return value of the contructor, while normally ethereal,
is actually usable for quite a while
second quirk: according to some conventions, the "m_" prefix indicates
a member variable
this doesn't jive with the provided syntax, but if the example had been
e.g.:
struct X {
std::vector<int> &m_vec;
X(): m_vec(std::vector<int>()) {}
};
a pitfall would have been revealed: the temporary goes out of scope
before the reference, so you end up with an invalid reference
see 12.2.5 of the standard for the exact language on temporaries and
references
regards,
michael toksvig
[ 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
|
|