Discussion:
What's the point of std::queue::pop() if I can't store the result?
(too old to reply)
elmazzun
2016-04-10 14:23:49 UTC
Permalink
Hi everybody, there is something that I don't understand
about popping from std::queue.
I pushed successfully a custom struct of mine in a queue,
like this:

data_packet pkt; // typedef'd struct of mine
q.push(pkt); // push() goes fine

What I don't get is how to store the result of a popped
element from my queue, since it is "void pop()".
I read about queue::front, which returns a reference to
"the oldest element in the queue and the same element that
is popped out from the queue when queue::pop is called."
Well, if front() returns a reference&, I made like this:

data_packet *pkt;
if (!q.empty()) {
pkt = q.front();
q.pop();
}

but the compiler says:
error: cannot convert 'data_pkt' to 'data_packet* {aka data_pkt*}' in assignment.

I think it returns this error because I pushed in my queue
a data_packet struct, not a pointer to it.
But, how can I retrieve a struct from my queue and
store it, not just pop() it and lose it?
Daniel
2016-04-10 14:55:52 UTC
Permalink
Post by elmazzun
I pushed successfully a custom struct of mine in a queue,
data_packet pkt; // typedef'd struct of mine
q.push(pkt); // push() goes fine
What I don't get is how to store the result of a popped
element from my queue, since it is "void pop()".
data_packet first = q.front();
q.pop();
Post by elmazzun
I read about queue::front, which returns a reference ...
data_packet *pkt;
if (!q.empty()) {
pkt = q.front();
q.pop();
}
If you replaced

data_packet *pkt;

with

data_packet& pkt;

it would compile, but it wouldn't work, as the reference would be
invalided by q.pop();

Daniel
elmazzun
2016-04-10 15:12:53 UTC
Permalink
Post by Daniel
data_packet first = q.front();
q.pop();
If you replaced
data_packet *pkt;
with
data_packet& pkt;
it would compile, but it wouldn't work, as the reference would be
invalided by q.pop();
That's what I was doing, indeed it compiled but the fields in my
retrieved struct shows orrible numbers.
Even commenting the pop() instruction did not make it work...
Öö Tiib
2016-04-10 15:49:37 UTC
Permalink
Post by elmazzun
Post by Daniel
data_packet first = q.front();
q.pop();
If you replaced
data_packet *pkt;
with
data_packet& pkt;
it would compile, but it wouldn't work, as the reference would be
invalided by q.pop();
That's what I was doing, indeed it compiled but the fields in my
retrieved struct shows horrible numbers.
Even commenting the pop() instruction did not make it work...
That sounds like your code has some more issues than that 'pop'.

The logic is that if copy constructor of popped element throws during
'pop' then first the implementation of 'pop' and after that you will
have quite bad situation. So no copies are made during 'pop'. Think
about it.

Therefore you should 'pop()' *after* you have done everything that you
want with the element. Including (if you need to) copying or moving
from it.

After 'pop' the element is gone and destroyed. Debug versions of some implementations also fill the memory where it was with most unlikely
garbage to ensure that it does not work and draws your attention.
elmazzun
2016-04-10 16:28:38 UTC
Permalink
You are right, I had to front() the struct from the queue
and ONLY after I read the values in the retrieved struct
pop() it.
Thanks to both of you, this was so stupid...still learning,
I'm from 2 years of C development and I still have to
master C++.

Stefan Ram
2016-04-10 15:04:31 UTC
Permalink
Post by elmazzun
data_packet *pkt;
if (!q.empty()) {
pkt = q.front();
A reference is not a pointer. »data_packet« needs some
constructor or assignment operator, and then one can write

data_packet pkt { q.font() }; /* C++17 */

or

pkt = q.front();

or maybe even

pkt = ::std::move( q.front() );

. Una referenza non è un puntatore. «data_packet» ha bisogna
di un construttore o un operatore di assegnamento, e poi si
può scrivere

data_packet pkt { q.font() }; /* C++17 */

oppure

pkt = q.front();

oppure magari

pkt = ::std::move( q.front );

.
Continue reading on narkive:
Search results for 'What's the point of std::queue::pop() if I can't store the result?' (Questions and Answers)
10
replies
What is AIX Box?
started 2006-05-08 15:58:44 UTC
hardware
Loading...