Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   iterator / returning reference to local temporary (http://www.velocityreviews.com/forums/t268036-iterator-returning-reference-to-local-temporary.html)

Alexander Stippler 07-04-2003 03:58 PM

iterator / returning reference to local temporary
 
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

Rob Williscroft 07-04-2003 04:30 PM

Re: iterator / returning reference to local temporary
 
Alexander Stippler wrote in news:3f05a4b5@news.uni-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/

Alexander Stippler 07-04-2003 04:40 PM

Re: iterator / returning reference to local temporary
 

> 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


All times are GMT. The time now is 01:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.