 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dave Rahardja Guest
|
Posted: Sat Oct 29, 2005 1:29 am Post subject: Const reference to a temporary object - Why? |
|
|
Hi all,
Although the following definition is legal:
const int& i = 5;
....and that the lifetime of the temporary variable to which i refers is
identical to i itself, why would anyone want to do this instead of a simple
const int i = 5;
....?
I can see how binding a const reference to a temporary object is necessary
(such as when passing an rvalue to a function expecting a const reference),
but the above usage perplexes me.
-dr
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Oct 29, 2005 3:24 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
Dave Rahardja wrote:
| Quote: | Although the following definition is legal:
const int& i = 5;
...and that the lifetime of the temporary variable to which i refers
is identical to i itself, why would anyone want to do this instead of
a simple
const int i = 5;
...?
|
You should ask somebody who did/does that.
| Quote: | I can see how binding a const reference to a temporary object is
necessary (such as when passing an rvalue to a function expecting a
const reference), but the above usage perplexes me.
|
There is nothing perplexing in that code by itself. It _would_ be
seriously perplexing in a production codebase. I have never seen
anything like that in production code.
V
|
|
| Back to top |
|
 |
Andrej Hristoliubov Guest
|
Posted: Sat Oct 29, 2005 3:36 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
| Quote: | I have never seen anything like that in production code.
V
|
Dave -
Do listen to Victor, I neither had seen this in production not
Victor's code. And I know Victor for quite some time now (Vitya is it
25 or 30 years in total is the duration of our acquaintance and
bestfriendship?).
|
|
| Back to top |
|
 |
Pete C Guest
|
Posted: Sat Oct 29, 2005 4:59 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
| Quote: | Dave -
Do listen to Victor, I neither had seen this in production not
Victor's code. And I know Victor for quite some time now (Vitya is it
25 or 30 years in total is the duration of our acquaintance and
bestfriendship?).
|
That is great advice. If anyone with your level of expertise in c++ has
experience in IRC, MIME and POP3 protocols I am happy to accept Job
Application for position in the US. Applicants must have skills of a
management of a team of the programmers.
|
|
| Back to top |
|
 |
Dave Rahardja Guest
|
Posted: Sat Oct 29, 2005 11:05 pm Post subject: Re: Const reference to a temporary object - Why? |
|
|
On Fri, 28 Oct 2005 23:24:40 -0400, "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net>
wrote:
| Quote: | Dave Rahardja wrote:
Although the following definition is legal:
const int& i = 5;
...and that the lifetime of the temporary variable to which i refers
is identical to i itself, why would anyone want to do this instead of
a simple
const int i = 5;
...?
You should ask somebody who did/does that.
|
I did, and his reasons (optimization) turned out to be misguided.
| Quote: | I can see how binding a const reference to a temporary object is
necessary (such as when passing an rvalue to a function expecting a
const reference), but the above usage perplexes me.
There is nothing perplexing in that code by itself. It _would_ be
seriously perplexing in a production codebase. I have never seen
anything like that in production code.
|
Neither have I, and I prevented one instance from creeping into our codebase
(by talking my colleague out of using it for (false) optimization).
What's perplexing is that such behavior as the lifetime of a temporary bound
to a const reference is defined at all. Stroustrup even gave an example of it
in C++PL (Section 5.5, pg 98 in 3rd ed). Seeing that the C++ language doesn't
seem to support constructs that are not either useful or maintain backward
compatibility, I'm wondering why such behavior is even defined at all.
I'm just trying to figure out the motivation behind the behavior, that's all.
-dr
|
|
| Back to top |
|
 |
Jonathan Mcdougall Guest
|
Posted: Sun Oct 30, 2005 12:47 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
Dave Rahardja wrote:
| Quote: | On Fri, 28 Oct 2005 23:24:40 -0400, "Victor Bazarov"
wrote:
Dave Rahardja wrote:
Although the following definition is legal:
const int& i = 5;
...and that the lifetime of the temporary variable to which i refers
is identical to i itself, why would anyone want to do this instead of
a simple
const int i = 5;
...?
You should ask somebody who did/does that.
I did, and his reasons (optimization) turned out to be misguided.
I can see how binding a const reference to a temporary object is
necessary (such as when passing an rvalue to a function expecting a
const reference), but the above usage perplexes me.
There is nothing perplexing in that code by itself. It _would_ be
seriously perplexing in a production codebase. I have never seen
anything like that in production code.
Neither have I, and I prevented one instance from creeping into our codebase
(by talking my colleague out of using it for (false) optimization).
What's perplexing is that such behavior as the lifetime of a temporary bound
to a const reference is defined at all. Stroustrup even gave an example of it
in C++PL (Section 5.5, pg 98 in 3rd ed). Seeing that the C++ language doesn't
seem to support constructs that are not either useful or maintain backward
compatibility, I'm wondering why such behavior is even defined at all.
I'm just trying to figure out the motivation behind the behavior, that's all.
|
Consistency.
Jonathan
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Oct 30, 2005 12:59 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
Dave Rahardja wrote:
| Quote: | On Fri, 28 Oct 2005 23:24:40 -0400, "Victor Bazarov"
[email]v.Abazarov (AT) comAcast (DOT) net[/email]> wrote:
Dave Rahardja wrote:
Although the following definition is legal:
const int& i = 5;
...and that the lifetime of the temporary variable to which i refers
is identical to i itself, why would anyone want to do this instead
of a simple
const int i = 5;
...?
[..]
I'm just trying to figure out the motivation behind the behavior,
that's all.
|
I don't understand what special motivation except consistency you
might need. If you think that initialising a const reference by
binding to a temporary (and thus prolonging the life of the object)
is OK when passing to a function or as a member of another object,
then why not stand-alone?
V
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Oct 30, 2005 2:12 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
* Victor Bazarov:
| Quote: | Dave Rahardja wrote:
On Fri, 28 Oct 2005 23:24:40 -0400, "Victor Bazarov"
[email]v.Abazarov (AT) comAcast (DOT) net[/email]> wrote:
Dave Rahardja wrote:
Although the following definition is legal:
const int& i = 5;
...and that the lifetime of the temporary variable to which i refers
is identical to i itself, why would anyone want to do this instead
of a simple
const int i = 5;
...?
[..]
I'm just trying to figure out the motivation behind the behavior,
that's all.
I don't understand what special motivation except consistency you
might need. If you think that initialising a const reference by
binding to a temporary (and thus prolonging the life of the object)
is OK when passing to a function or as a member of another object,
then why not stand-alone?
|
The point is that for a function call, or in an initializer list, and
especially in a function return, the temporary's life is not prolonged.
C++ adds _special_ support for local references to const. And then one
can view binding in function calls / init lists as just a consequence,
but why allow binding rvalues to local references in the first place?
The rules would be just as simple if only binding to arguments was
allowed for rvalues. I think the rules would be _simpler_, and also C++
compilation, and it would, I think, help to detect some obscure bugs.
I've tried to think of optimization scenarios, e.g. an 'extern "C"'
function returning a huge C struct, but no, the feature doesn't seem to
be able to help the compiler.
Perhaps the explanation is the same as (this is what's been stated by
folks Who Should Know) for the non-support of elision of copy
construction for arguments: some committee member(s) had or knew of
code, perhaps some company's zillion line application or perhaps a
popular compiler, that depended on having the language the way it's now
standardized.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
Dave Rahardja Guest
|
Posted: Sun Oct 30, 2005 2:07 pm Post subject: Re: Const reference to a temporary object - Why? |
|
|
On Sun, 30 Oct 2005 02:12:52 GMT, [email]alfps (AT) start (DOT) no[/email] (Alf P. Steinbach) wrote:
| Quote: | The point is that for a function call, or in an initializer list, and
especially in a function return, the temporary's life is not prolonged.
C++ adds _special_ support for local references to const. And then one
can view binding in function calls / init lists as just a consequence,
but why allow binding rvalues to local references in the first place?
The rules would be just as simple if only binding to arguments was
allowed for rvalues. I think the rules would be _simpler_, and also C++
compilation, and it would, I think, help to detect some obscure bugs.
|
Right. The special extension of the temporary's lifetime is what got me
scratching my head.
Oh well, it may be one of those things that "just are".
-dr
|
|
| Back to top |
|
 |
bjarne Guest
|
Posted: Sun Oct 30, 2005 5:21 pm Post subject: Re: Const reference to a temporary object - Why? |
|
|
The rules for references are simply the most general and uniform I
could find. In the cases of arguments and local references, the
temporary lives as long as the reference to which it is bound. One
obvious use is as a shorthand for a complicated expression in a
deeplynested loop. For example:
for (int i = 0; i
for (int j = 0; j< ymax; ++j) {
double& r = a[i][j];
for (int k = 0; k < zmax; ++k) {
// do something with a[i][j] and a[i][j][k]
}
}
This can improve readability as well as run-time performance.
-- Bjarne Stroustrup; http://www.research.att.com/~bs
|
|
| Back to top |
|
 |
STOP Guest
|
Posted: Sun Oct 30, 2005 7:51 pm Post subject: Re: Const reference to a temporary object - Why? |
|
|
How exactly would the readability and run-time performance be affected
if we used a regular auto?
double r = a[i][j];
I find the above line more readable - not because it's one char shorter
than the reference-using one but because it raises no questions and
head-scratching. And as far as performace goes I thought modern
compilers could handle the described case just fine. I would really
like to hear your clarification though!
Thanks!
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Oct 30, 2005 8:49 pm Post subject: Re: Const reference to a temporary object - Why? |
|
|
* STOP:
| Quote: |
double r = a[i][j];
|
You can not assign to the array element via that 'r', so it does not do
the same job as the reference you think it replaces.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Oct 30, 2005 8:54 pm Post subject: Re: Const reference to a temporary object - Why? |
|
|
* bjarne:
| Quote: |
The rules for references are simply the most general and uniform I
could find.
|
I guess it boils down to the subjectively "simple", then. Thanks for
the explanation.
| Quote: | In the cases of arguments and local references, the
temporary lives as long as the reference to which it is bound. One
obvious use is as a shorthand for a complicated expression in a
deeplynested loop. For example:
for (int i = 0; i
for (int j = 0; j< ymax; ++j) {
double& r = a[i][j];
for (int k = 0; k < zmax; ++k) {
// do something with a[i][j] and a[i][j][k]
}
}
This can improve readability as well as run-time performance.
|
Yes, but it does not illustrate a case where binding a local reference
to const, to an rvalue, is of any direct practical value -- so the
value of that is presumably only that it provides a simple, general set
of rules?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
bjarne Guest
|
Posted: Mon Oct 31, 2005 12:18 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
A simple, general, set of rules is the ideal. As far as possible, the
rules for T& and const T& are the same as are the rules for T& and U&.
However, modify my example a bit
for (int i = 0; i<xmax; ++i)
for (int j = 0; j< ymax; ++j) {
const vector
for (int k = 0; k < zmax; ++k) {
// do something with a[i][j] and a[i][j][k]
}
}
You still have the notational advantage, and we wouldn't want to write
vector
and copy 1000 elements. Obviously, it is also more realistic to have
a[i][j] a vector than a double (since I proceeded to subscript it :-)
-- Bjarne Stroustrup; http://www.research.att.com/~bs
|
|
| Back to top |
|
 |
STOP Guest
|
Posted: Mon Oct 31, 2005 1:12 am Post subject: Re: Const reference to a temporary object - Why? |
|
|
| Quote: | You can not assign to the array element via that 'r', so it does not do
the same job as the reference you think it replaces.
|
Oh, but.. yes, of course - how very silly of me!
I hope *nobody* saw this utterly shameful slip-up )
...exqueezemoi eurobody
|
|
| 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
|
|