C++ Templates

Discussion in 'C++' started by huangshan, Oct 23, 2006.

  1. huangshan

    huangshan Guest

    hi all:

    i am reading "C++ Templates: The Complete Guide"

    in "2.4 Overloading Function Templates",
    he say "This is because for C-strings, max(a,b) creates a new, temporary
    local value that may be returned by the function by reference."

    i can't understand that why in this condition will becomes an error.
    huangshan, Oct 23, 2006
    #1
    1. Advertising

  2. huangshan wrote:
    > i am reading "C++ Templates: The Complete Guide"
    >
    > in "2.4 Overloading Function Templates",
    > he say "This is because for C-strings, max(a,b) creates a new,
    > temporary local value that may be returned by the function by
    > reference."
    > i can't understand that why in this condition will becomes an error.


    Returning a reference to a [local] temporary object is something you
    should consider avoiding since the reference will outlive the object
    and will become invalid before the function even returns it. Any
    use of that reference will have undefined behaviour.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 23, 2006
    #2
    1. Advertising

  3. huangshan

    huangshan Guest

    can you give me a example ?

    thanks
    huangshan, Oct 23, 2006
    #3
  4. huangshan

    Ye Dafeng Guest

    huangshan wrote:
    > can you give me a example ?
    >
    > thanks
    >
    >


    char* getVoid()
    {
    char temp = 'c';

    return &temp;
    }

    in this example, the temp address may be used for other object. If you
    use it, you will get another result which you do not expect.
    Ye Dafeng, Oct 23, 2006
    #4
  5. huangshan

    Martin Steen Guest

    huangshan wrote:
    > can you give me a example ?
    >
    > thanks
    >
    >


    Don't do this:

    int& Function1()
    {
    int a = 100;
    return a; // g++ warning: reference to local variable 'a' returned
    // When Function1 ends, the reference to a is no longer
    // valid, because a is local.
    }

    void Function2()
    {
    cout << Function1() << endl;
    // output is not "100", but something else!
    }

    Best regards,
    -Martin
    Martin Steen, Oct 23, 2006
    #5
  6. huangshan

    Greg Comeau Guest

    In article <ehikci$l4s$99.com>,
    Ye Dafeng <> wrote:
    >huangshan wrote:
    >> can you give me a example ?
    >>
    >> thanks
    >>
    >>

    >
    >char* getVoid()
    >{
    > char temp = 'c';
    >
    > return &temp;
    >}
    >
    >in this example, the temp address may be used for other object. If you
    >use it, you will get another result which you do not expect.


    Right, as Victor mentioned, temp is not guaranteed to exist once
    getVoid is over with. Therefore, to return its adress to be used
    as if it still exists is asking for trouble. This is another facet
    of something I term "good garbage" because it is something that
    may appear to work, but use that pointer 10 minutes lates and
    it may not be pointing at temp any longer but some other variable
    which has taken its place, and so on.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Oct 23, 2006
    #6
  7. huangshan

    loufoque Guest

    Ye Dafeng wrote:
    > huangshan wrote:
    >> can you give me a example ?
    >>
    >> thanks
    >>
    >>

    >
    > char* getVoid()
    > {
    > char temp = 'c';
    >
    > return &temp;
    > }


    This is not what he asked for.
    What he asked for is :

    char& getVoid()
    {
    char temp = 'c';

    return temp;
    }
    loufoque, Oct 23, 2006
    #7
  8. huangshan

    huangshan Guest

    thanks
    Victor Bazarov ,
    Ye Dafeng,
    Greg Comeau,
    loufoque
    and Martin Steen

    i see
    huangshan, Oct 24, 2006
    #8
    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. Fred
    Replies:
    1
    Views:
    597
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    380
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    468
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    364
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    800
    recover
    Jul 25, 2006
Loading...

Share This Page