Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iterator / returning reference to local temporary

Reply
Thread Tools

iterator / returning reference to local temporary

 
 
Alexander Stippler
Guest
Posts: n/a
 
      07-04-2003
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:air<int,int> &
dummy(int key) {
return *(hash.find(key));
}

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


regards,
Alex
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-04-2003
Alexander Stippler wrote in news:(E-Mail Removed)-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:air<int,int> &
> dummy(int key) {
> return *(hash.find(key));
> }
>
> int
> main()
> {
> hash[8] = 9;
> dummy(;
> return 0;
> }
>
>


You may find that std::hash_map<> (which is non-standard BTW)
actually stores a std:air<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/
 
Reply With Quote
 
 
 
 
Alexander Stippler
Guest
Posts: n/a
 
      07-04-2003

> You may find that std::hash_map<> (which is non-standard BTW)
> actually stores a std:air<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
 
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
returning a (const) reference to a temporary AdlerSam C++ 10 02-22-2011 01:02 PM
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Returning a reference to a local variable pauldepstein@att.net C++ 9 12-30-2007 03:51 PM
Iterator returning a reference to object of an abstract class? Belebele C++ 3 02-07-2007 09:32 PM
Returning a reference to a temporary marco_segurini C++ 1 01-10-2005 04:25 PM



Advertisments