C ++ std :: queue :: pop()调用析构函数。什么指针类型? [英] C++ std::queue::pop() calls destructor. What of pointer types?

查看:1933
本文介绍了C ++ std :: queue :: pop()调用析构函数。什么指针类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 std :: queue ,作为模板类包装成一个线程安全的队列。我有两个版本的这个类:一个存储值类型,一个存储指针类型。

I have a std::queue that is wrapped as a templated class to make a thread-safe queue. I have two versions of this class: one that stores value types, one that stores pointer types.

对于指针类型,我有麻烦删除队列在销毁。原因是我不知道如何从队列中安全地移除项目。

For the pointer type, I'm having trouble deleting the elements of the queue on destruction. The reason is that I don't know a way to remove the items from the queue safely.

这个引用状态(空,所以我想它实际上不是STATE它),从队列中删除元素的唯一方法是调用 pop()。该参考还说, pop()调用项目的析构函数。

This reference states (vacuously, so I guess it doesn't actually STATE it) that the only way to remove elements from the queue is to call pop(). The reference also says that pop() calls the destructor for the item.

指针类型,因为它们可能或可能不会实际指向聚合。如果其中一个指向一个聚合,他们都会,但因为包装器是模板,不能保证我们正在处理哪种类型(聚合或非聚合)。

Well, this causes problems with my pointer types because they may or may not actually point to aggregates. If one of them points to an aggregate, they all will, but because the wrapper is templated, there is no guarantee which type (aggregated or non-aggregated) we are dealing with.

所以,当 pop()调用析构函数时,会发生什么?如何确保一切正在删除,并正确地分配内存?

So, when pop() calls the destructor, what happens? How do I ensure that everything is being removed and the memory deallocation properly?

最后,我的解决方案是使用较旧版本的GCC for ARM9。我不能控制这一点。我知道有图书馆有智能指针和容器,这将有助于这里,但他们是对我的限制。

Lastly, my solution is using an older version of GCC for ARM9. I don't have control over this. I understand that there are libraries that have smart pointers and containers that would assist here, but they are off-limits for me.

推荐答案

在线来源值得您为他们付费 - 获得适当的参考资料,如 Josuttis的书。 pop()没有调用析构函数 - 它只是通过调用pop_front()从队列适配器的底层表示(默认为std :: deque)中删除一个元素。如果被弹出的东西有一个析构函数,当弹出的对象超出范围,但是队列类与它无关时,它将被使用。

Online sources are worth what you pay for them - get a proper reference like Josuttis's book. pop() does not "call the destructor" - it simply removes an element from the queue adaptor's underlying representation (by default a std::deque) by calling pop_front() on it. If the thing being popped has a destructor, it will be used when the popped object goes out of scope, but the queue class has nothing to do with it.

这篇关于C ++ std :: queue :: pop()调用析构函数。什么指针类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆