 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ThosRTanner Guest
|
Posted: Wed Sep 28, 2005 2:27 am Post subject: Compiler generated temporaries |
|
|
Given this:
class A { std::string x; public: std::string get_name() { return x; }
};
class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };
.....
A a;
....
B b(a.get_name());
is the value of b: well defined - or is it taking a reference to a
temporary which gets deleted? (And if so, why doesn't the compiler tell
me!?)
Thanks
[ 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 Sep 28, 2005 11:05 am Post subject: Re: Compiler generated temporaries |
|
|
ThosRTanner wrote:
| Quote: | Given this:
class A { std::string x; public: std::string get_name() { return x; }
};
class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };
....
A a;
...
B b(a.get_name());
is the value of b: well defined - or is it taking a
reference to a temporary which gets deleted?
|
It's a dangling reference; using it is undefined behavior.
| Quote: | (And if so, why doesn't the compiler tell me!?)
|
Because you've spread the problem out over several different
functions, and the compiler, which normally only looks at one
function at a time, can't see it.
--
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 |
|
 |
Emil Kirichev Guest
|
Posted: Wed Sep 28, 2005 11:07 am Post subject: Re: Compiler generated temporaries |
|
|
The reference will be invalid when the destructor of A is called. So,
as far as 'a' exists, 'b' is in a good state. Kinda dangerous.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ravinderthakur@gmail.com Guest
|
Posted: Wed Sep 28, 2005 11:08 am Post subject: Re: Compiler generated temporaries |
|
|
this is not a legal way of doing it!!!
you will be storing an object that would have been destroyed.
the compiler cannot detect such errors because the compiler
does only syntax checking and not the semantic checking.
in ur code the syntax is write and only the semantic is wrong.
thanks
rt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Wed Sep 28, 2005 11:08 am Post subject: Re: Compiler generated temporaries |
|
|
ThosRTanner wrote:
| Quote: | Given this:
class A { std::string x; public: std::string get_name() { return x; }
};
class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };
....
A a;
...
B b(a.get_name());
is the value of b: well defined
|
When? Till the end of this expression it is well defined and refers to
the temporary that is returned by get_name.
After than, b.x becomes a dangling reference, because there is nothing
that would force that temporary to live longer that this single expression.
| Quote: | is it taking a reference to a
temporary which gets deleted?
|
Right.
| Quote: | (And if so, why doesn't the compiler tell
me!?)
|
Good question. The standard does not prevent the compiler from telling
you, so the compiler *might* warn you about this. On the other hand, the
standard also does not prevent you from writing this kind of code, and
in general it is very difficult (or even impossible) to analyse all such
cases. Here it looks straightforward, because everything is in the same
file, but imagine that B's ctor is compiled separately...
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yuri Khan Guest
|
Posted: Wed Sep 28, 2005 11:09 am Post subject: Re: Compiler generated temporaries |
|
|
ThosRTanner wrote:
| Quote: | Given this:
class A { std::string x; public: std::string get_name() { return x; }
};
class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };
....
A a;
...
B b(a.get_name());
is the value of b: well defined - or is it taking a reference to a
temporary which gets deleted? (And if so, why doesn't the compiler tell
me!?)
|
A little experimentation shows that the reference is valid until the
end of B's constructor. After that, the temporary gets deleted, and the
reference is left dangling.
C++ Standard, 2003, 12.2/5:
The temporary to which the reference is bound or the temporary that is
the complete object to a subobject of which the temporary is bound
persists for the lifetime of the reference except as specified below. A
temporary bound to a reference member in a constructor's
ctor-initializer (12.6.2) persists until the constructor exits.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ThosRTanner Guest
|
Posted: Wed Sep 28, 2005 1:51 pm Post subject: Re: Compiler generated temporaries |
|
|
Thanks for all the helpful responses. BTW, you'll be glad to hear this
wasn't my code, I was trying to work out what was going wrong (but only
going wrong sometimes!).
[ 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
|
|