C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

std::swap() not specialised for container adaptors (23.2.3)?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Falk Tannhäuser
Guest





PostPosted: Thu Mar 24, 2005 4:34 am    Post subject: std::swap() not specialised for container adaptors (23.2.3)? Reply with quote



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 ]

Back to top
Carl Barron
Guest





PostPosted: Sat Apr 02, 2005 5:36 am    Post subject: Re: std::swap() not specialised for container adaptors (23.2 Reply with quote



Falk Tannhäuser <falk.tannhauser (AT) crf (DOT) canon.fr> wrote:

Quote:
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


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 = std::vector
struct swapable_stack:std::stack<T,C>
{
explicit swapble_stack(const C &cont=C()):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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.