473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
Jul 19 '05 #1
2 3713
Alexander Stippler wrote in news:3f******@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/
Jul 19 '05 #2
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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew Fleet | last post by:
Hi, I'm looking at returning a reference to an array I create within a subroutine. I could do this... sub foo { my @theArray; <snip>
6
by: JKop | last post by:
unsigned int CheesePlain(void) { unsigned int const chalk = 42; return chalk; } unsigned int& CheeseRef(void) {
19
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
7
by: sam | last post by:
Hi, This is a "list iterator" problem I expect it will copy the list iterator (l_iter) to the caller: eg. list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i) {...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
6
by: EvilOldGit | last post by:
const Thing &operator++(int) { Thing temp = *this; operator++(); return temp; } Is this code robust ? I get a compiler warning about returning a reference to a a local, which I guess is...
7
by: Jess | last post by:
Hello, I learned that if we do "v.end()", then the returned iterator is a temporary object and hence cannot be changed like --v.end(); Why is the returned iterator a temporary pointer? I...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.