iterator / returning reference to local temporary

Discussion in 'C++' started by Alexander Stippler, Jul 4, 2003.

  1. Hi,

    I've got a question concerning iterators and STL. Why does the compiler
    warn, that function dummy in the following example returns a reference
    to a local temporary? In my understanding, it is not temporary. It's an
    entry in a hash_map and therefore not temporary. Where am I wrong? How do I
    get the desired functionality?
    And by the way: Does anybody know a real in-depth reference and programming
    guide for understanding and above all extending the STL by own data types?

    #include <hash_map>
    #include <pair.h>
    #include <iostream>

    std::hash_map<int,int> hash;

    const std::pair<int,int> &
    dummy(int key) {
    return *(hash.find(key));
    }

    int
    main()
    {
    hash[8] = 9;
    dummy(8);
    return 0;
    }


    regards,
    Alex
    Alexander Stippler, Jul 4, 2003
    #1
    1. Advertising

  2. Alexander Stippler wrote in news:-ulm.de:

    > Hi,
    >
    > I've got a question concerning iterators and STL. Why does the
    > compiler warn, that function dummy in the following example returns a
    > reference to a local temporary? In my understanding, it is not
    > temporary. It's an entry in a hash_map and therefore not temporary.
    > Where am I wrong? How do I get the desired functionality?
    > And by the way: Does anybody know a real in-depth reference and
    > programming guide for understanding and above all extending the STL by
    > own data types?
    >
    > #include <hash_map>
    > #include <pair.h>
    > #include <iostream>
    >
    > std::hash_map<int,int> hash;
    >
    > const std::pair<int,int> &
    > dummy(int key) {
    > return *(hash.find(key));
    > }
    >
    > int
    > main()
    > {
    > hash[8] = 9;
    > dummy(8);
    > return 0;
    > }
    >
    >


    You may find that std::hash_map<> (which is non-standard BTW)
    actually stores a std::pair<int const, int>. If it were otherwise
    you could rewrite the key without the map knowing about it.

    Try using:

    std::hash_map<int,int>::value_type &

    as your return type.

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jul 4, 2003
    #2
    1. Advertising


  3. > You may find that std::hash_map<> (which is non-standard BTW)
    > actually stores a std::pair<int const, int>. If it were otherwise
    > you could rewrite the key without the map knowing about it.
    >
    > Try using:
    >
    > std::hash_map<int,int>::value_type &
    >
    > as your return type.
    >
    > HTH
    >
    > Rob.


    Thanks, but your solution is non-standard BTW, too. ;-))
    Should be
    typename std::hash_map<int,int>::value_type &
    ^^^^^^^^

    regards,
    Alex
    Alexander Stippler, Jul 4, 2003
    #3
    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. marco_segurini

    Returning a reference to a temporary

    marco_segurini, Jan 10, 2005, in forum: C++
    Replies:
    1
    Views:
    349
    Dietmar Kuehl
    Jan 10, 2005
  2. Belebele
    Replies:
    3
    Views:
    321
    Belebele
    Feb 7, 2007
  3. Replies:
    9
    Views:
    463
    Ron Natalie
    Dec 30, 2007
  4. Replies:
    7
    Views:
    3,178
    James Kanze
    Feb 12, 2008
  5. AdlerSam
    Replies:
    10
    Views:
    2,932
    James Kanze
    Feb 22, 2011
Loading...

Share This Page