Topic: std::swap() not specialised for container adaptors (23.2.3)?
Author: cbarron3@ix.netcom.com (Carl Barron)
Date: Sat, 2 Apr 2005 05:36:18 GMT Raw View
Falk Tannh=E4user <falk.tannhauser@crf.canon.fr> wrote:
> All the standard containers have a swap() member function, and
> for all of them, std::swap() is specialised to call said member
> function. However, no equivalent exists for the adaptors described
> in =A7 23.2.3 (std::queue, std::priority_queue, std::stack) - thus,
> while std::swap() works for them, it will use copy construction
> and assignment (these are not explicitly defined for adaptors
> in the Standard either, so the compiler-generated ones are used).=20
> This leads both to less-than-optimal performance and to the
> possibility of throwing exceptions.
> Was there a deliberate decision not to specialise std::swap()
> for adaptors (it would just have to call swap() for the underlying
> containers, and in case of std::priority_queue for the comparison
> object) or has it just been forgotten?
>=20
> Falk
>=20
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.
example:
template <class T,class C =3D std::vector<T> >
struct swapable_stack:std::stack<T,C>
{
explicit swapble_stack(const C &cont=3DC()):std::stack<T,C>(cont)
{}
void swap(swapable_stack &r)
{
c.swap(r.c);
}
};
template <class T,class C>
inline
void swap(swapable_stack<T,C> &x,swapable_stack<T,C> &y)
{ x.swap(y);}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: =?ISO-8859-1?Q?Falk_Tannh=E4user?= <falk.tannhauser@crf.canon.fr>
Date: Wed, 23 Mar 2005 22:34:56 CST Raw View
All the standard containers have a swap() member function, and
for all of them, std::swap() is specialised to call said member
function. However, no equivalent exists for the adaptors described
in 23.2.3 (std::queue, std::priority_queue, std::stack) - thus,
while std::swap() works for them, it will use copy construction
and assignment (these are not explicitly defined for adaptors
in the Standard either, so the compiler-generated ones are used).
This leads both to less-than-optimal performance and to the
possibility of throwing exceptions.
Was there a deliberate decision not to specialise std::swap()
for adaptors (it would just have to call swap() for the underlying
containers, and in case of std::priority_queue for the comparison
object) or has it just been forgotten?
Falk
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]