Question about "auto"

Discussion in 'C++' started by fungus, Apr 24, 2010.

  1. fungus

    fungus Guest

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

    eg. Will this code print "2"?

    int x = 1;
    auto& y = x;
    y = 2;
    fungus, Apr 24, 2010
    #1
    1. Advertising

  2. fungus

    fungus Guest

    Oops, typo:

    Code should read:

    int x = 1;
    auto& y = x;
    y = 2;
    cout << x;
    fungus, Apr 24, 2010
    #2
    1. Advertising

  3. fungus <> 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
    Paul Bibbings, Apr 24, 2010
    #3
  4. fungus <> 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
    Paul Bibbings, Apr 24, 2010
    #4
  5. fungus

    fungus Guest

    On Apr 24, 11:49 pm, Stuart Golodetz <> 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.
    fungus, Apr 24, 2010
    #5
  6. fungus

    fungus Guest

    On Apr 24, 11:38 pm, Paul Bibbings <> 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...
    fungus, Apr 24, 2010
    #6
  7. fungus

    fungus Guest

    On Apr 24, 11:45 pm, Paul Bibbings <> 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!
    fungus, Apr 24, 2010
    #7
  8. fungus <> writes:

    > On Apr 24, 11:49 pm, Stuart Golodetz <> 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
    Paul Bibbings, Apr 25, 2010
    #8
  9. fungus <> writes:

    > On Apr 24, 11:38 pm, Paul Bibbings <> 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
    Paul Bibbings, Apr 25, 2010
    #9
  10. fungus

    fungus Guest

    On Apr 25, 12:34 pm, Paul Bibbings <> wrote:
    > fungus <> writes:
    > > On Apr 24, 11:49 pm, Stuart Golodetz <> 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.
    fungus, Apr 25, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    1
    Views:
    359
    Natty Gur
    Jul 17, 2003
  2. VB Programmer
    Replies:
    1
    Views:
    652
    William F. Robertson, Jr.
    Sep 30, 2003
  3. =?Utf-8?B?V2FyYW4=?=

    Auto-Suggested Textbox like google auto suggest

    =?Utf-8?B?V2FyYW4=?=, Apr 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,510
    inrakeshworld
    Jul 27, 2007
  4. ^_^
    Replies:
    2
    Views:
    374
    Ben Pfaff
    Jan 6, 2004
  5. linkswanted
    Replies:
    1
    Views:
    897
Loading...

Share This Page