Question about "auto"

F

fungus

Can the "auto" keyword be used to create a reference?

eg. Will this code print "2"?

int x = 1;
auto& y = x;
y = 2;
 
P

Paul Bibbings

fungus said:
Oops, typo:

Code should read:

int x = 1;
auto& y = x;
y = 2;
cout << x;

Assuming that we are not required to wait for the "Oops, typo: code
should be wrapped in something that will actually compile; e.g., a main,"
followed by a "Oops, typo: code should include a header for the iostream
lib," and then the "Oops, typo: code should apply some means to resolve
`cout' to the namespace it is defined in" ... :)

22:35:04 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano $cat auto_ref.cpp
// file: auto_ref.cpp

#include <iostream>

int main()
{
int x = 1;
auto& y = x;
y = 2;
std::cout << x;
}


22:35:11 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano $i686-pc-cygwin-g++-4.5.0 -std=c++0x
auto_ref.cpp

22:35:35 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano $./a
2

.... then the answer to your original question is, apparently, yes. (Or,
at least, gcc thinks so.)

Regards

Paul Bibbings
 
P

Paul Bibbings

fungus said:
Oops, typo:

Code should read:

int x = 1;
auto& y = x;
y = 2;
cout << x;

To follow up the "yes" answers from VS2010 and gcc-4.5.0, the detail is
in [dcl.spec.auto] 7.1.6.4, where it has this example:

const auto &i = expr;

Regards

Paul Bibbings
 
F

fungus

This seems more reliable than the "engineer's induction" I had in
mind... :)

I already did the "engineer's induction" ... but I've
already learned that C++ doesn't work that way.
 
F

fungus

Assuming that we are not required to wait for the "Oops, typo: code
should be wrapped in something that will actually compile; e.g., a main,"
followed by a "Oops, typo: code should include a header for the iostream
lib," and then the "Oops, typo: code should apply some means to resolve
`cout' to the namespace it is defined in" ...  :)

I was just trying to keep it ASAP...
 
P

Paul Bibbings

fungus said:
I already did the "engineer's induction" ... but I've
already learned that C++ doesn't work that way.

I remember way back (well, not /that/ way) when this lesson finally sunk
in. Before that, when I was probably coding in C mostly, I can actually
recall that I had somehow grounded my learning process in "give it a
try; play around with it until it does what you want; then, if it does,
you learnt something (documentation as a last resort)." Today, I can
hardly believe it, having discovered along the way that there are such
things as compiler idiosyncracies, bugs, standard non-compliance,
non-standard extensions and, of course, undefined behaviour, and if it
weren't for the fact that I have become rehabilited I'd probably still
be a little embarrassed.

The odd thing, though, is that I am finding the temptation is trying to
raise its head again, and I think I've worked out why. Basically, it's
Comeau's fault ;-). Somehow, the trust that others, and myself, are
able to put in this as a highly standard-conforming implementation is
somehow threatening to undo all the good work. It is a temptation that
is hard to resist for someone with my beginnings, and I find myself
having to fight against a tendency to do it the `engineer's' way "for old
times' sake." "We have Comeau; if that says it's right, it's right," a
little voice keeps wanting to say. And this, at a time when I am trying
to get all the understanding I can out of the `documentation' as the
"one true way."

Perverse little imps we carry around inside of us. ;-)

Regards

Paul Bibbings
 
F

fungus

I remember way back (well, not /that/ way) when this lesson
finally sunk in.

I think I finally stopped doing it around VC++ 2005
when it turned out that iterators aren't pointers
after all. And, no, you cant set them to NULL or
test them for NULL either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top