为什么std队列不定义交换方法专门化 [英] Why does std queue not define a swap method specialisation

查看:179
本文介绍了为什么std队列不定义交换方法专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过,所有的stl容器提供了交换算法的专业化,以避免调用复制构造函数和默认方法使用的两个赋值操作。但是,当我认为在一些代码中使用队列是很好的我正在工作我注意到(不像矢量和deque)队列不提供这种方法?我只是决定使用deque而不是队列,但仍然有兴趣知道为什么是这样的。

I've read that all stl containers provide a specialisation of the swap algorithm so as to avoid calling the copy constructor and two assignment operations that the default method uses. However, when I thought it would be nice to use a queue in some code I was working on I noticed that (unlike vector and deque) queue doesn't provide this method? I just decided to use a deque instead of a queue, but still I'm interested to know why this is?

推荐答案

a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2738.pdf =nofollow> C ++ 0x 将添加交换到容器适配器如std :: queue。我只能推测为什么它从当前标准缺失。
此讨论有人提出了一个解决方法:

C++0x will add swap to container adapters like std::queue. I could only speculate why it is missing from the current standard. In this discussion someone proposes a workaround:


有一个解决方案,因为标准使所需的部分受到保护,
称为继承。 [只是不通过std适配器销毁]
创建一个模板化的结构继承所需的适配器,只提供
的构造函数,并将args转发给适配器类,编写一个
swap成员是因为必需的项目是受保护的成员
标准适配器。

There is a solution since the standard makes the needed parts protected, called inheritance. [just don't destruct via the std adaptors] create a templated struct inheriting the desired adaptor, provide just the constructors and forward the args to the adaptor class, writing a swap member is a snap since the required items are protected members of the standard adaptors.

这篇关于为什么std队列不定义交换方法专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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