Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Const reference to a temporary object - Why?

Reply
Thread Tools

Const reference to a temporary object - Why?

 
 
STOP
Guest
Posts: n/a
 
      10-30-2005
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!

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-30-2005
* STOP:
>
> 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?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-30-2005
* bjarne:
>
> 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.


>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<xmax; ++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?
 
Reply With Quote
 
bjarne
Guest
Posts: n/a
 
      10-31-2005
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<double>& r = a[i][j];
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<double> r = a[i][j];

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

 
Reply With Quote
 
STOP
Guest
Posts: n/a
 
      10-31-2005
>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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-03-2005
* bjarne:
> 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<double>& r = a[i][j];
> 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<double> r = a[i][j];
>
> 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


Heh... I didn't even notice that type-o, I guess because that
wasn't what you tried to communicate.

However, the above isn't an example of the temporary lifetime extension,
either.

Somewhere there must be an example of practical usefulness, I'm sure!

On the third hand, I just stumbled over an issue seemingly a consequence
of this general idea of _generating_ a tempory to bind a reference to,
namely that with the current standard the following should not compile,
and indeed does not compile with g++ 3.4.4, nor with Comeau Online
4.3.3, because of that generated temporary requiring copy construction:

#include <memory>

struct E {};

typedef std::auto_ptr<E> EPtr;

EPtr foo() { return EPtr( new E ); }
EPtr bar( EPtr const& ) { return EPtr( new E ); }

int main()
{
bar( foo() ); // Oops! Not allowed by current rules!
}

After a bit of searching I found that this utter silliness, which also
was a stumbling block for Andrei's Mojo, has been adressed by core issue
391, <url:
http://www2.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#391>, and
"voted into WP" (that's C++0x, isn't it?), and that's nice.

But that doesn't help us until C++0x, which is -- when?


Cheers,

- Alf

--
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
temporary object and const reference zade C++ 5 11-15-2012 09:35 PM
non-const reference to temporary object rajath575@gmail.com C++ 10 11-27-2008 05:54 PM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
A reference to non-const to be bound to a temporary object John Ky C++ 9 02-23-2004 12:53 AM



Advertisments