Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about "auto"

Reply
Thread Tools

Question about "auto"

 
 
fungus
Guest
Posts: n/a
 
      04-24-2010
Can the "auto" keyword be used to create a reference?

eg. Will this code print "2"?

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



 
Reply With Quote
 
 
 
 
fungus
Guest
Posts: n/a
 
      04-24-2010
Oops, typo:

Code should read:

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

 
Reply With Quote
 
 
 
 
Paul Bibbings
Guest
Posts: n/a
 
      04-24-2010
fungus <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      04-24-2010
fungus <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
fungus
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 11:49*pm, Stuart Golodetz <(E-Mail Removed)> wrote:
>
> 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.


 
Reply With Quote
 
fungus
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 11:38*pm, Paul Bibbings <(E-Mail Removed)> wrote:
> 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...

 
Reply With Quote
 
fungus
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 11:45*pm, Paul Bibbings <(E-Mail Removed)> wrote:
>
> 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:
>


OK, thanks!


 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      04-25-2010
fungus <(E-Mail Removed)> writes:

> On Apr 24, 11:49*pm, Stuart Golodetz <(E-Mail Removed)> wrote:
>>
>> 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.


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
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      04-25-2010
fungus <(E-Mail Removed)> writes:

> On Apr 24, 11:38*pm, Paul Bibbings <(E-Mail Removed)> wrote:
>> 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...


I know. I was being unfair in my jesting.

Regards

Paul Bibbings
 
Reply With Quote
 
fungus
Guest
Posts: n/a
 
      04-25-2010
On Apr 25, 12:34*pm, Paul Bibbings <(E-Mail Removed)> wrote:
> fungus <(E-Mail Removed)> writes:
> > On Apr 24, 11:49 pm, Stuart Golodetz <(E-Mail Removed)> wrote:

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

>
> > I 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.


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.


 
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
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments