* 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.htm l#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?