Hello everyone,
All swap functions in STL container class is exception safe and nothrow, right? I have searched for swap for vector and deque, but seems no explicit document about the exception safe level.
thanks in advance,
George
Hello everyone,
All swap functions in STL container class is exception safe and nothrow, right? I have searched for swap for vector and deque, but seems no explicit document about the exception safe level.
thanks in advance,
George
The 2003 edition of C++ Standard states:
Originally Posted by Section 23.1
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The associative containers have a Compare object since they internally order their elements. This Compare object has a copy constructor and an assignment operator. Since vector and deque do not have Compare objects, their swap functions are guaranteed not to throw.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks laserlight,
I think you quoted C++ Standard implies that Compare object will be used for some containers when we do swap.
My question, why compare object is needed if we want to swap content of two container? Compare I think it means something like greater than, less than, etc.
regards,
George
You have to swap the Compare objects too.My question, why compare object is needed if we want to swap content of two container? Compare I think it means something like greater than, less than, etc.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hi laserlight,
I only know comparasion operator, and I do not know what is Compare object. What is Compare object (I think compare object is some special member of a class in your context)?
I searched Google and wikipedia, but no result for "Compare object C++". It is appreciated if you could share some links about it or give your description please?
regards,
George
Take for example std::map:I only know comparasion operator, and I do not know what is Compare object. What is Compare object (I think compare object is some special member of a class in your context)?
The Compare object is just some function object that is used to compare the keys of the map such that they can be ordered.Code:template <class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key, T> > > class map { public: // ... typedef Compare key_compare; // ... protected: class value_compare : public binary_function<value_type,value_type,bool> { friend class map; protected: Compare comp; value_compare(Compare c) : comp(c) {} public: bool operator()(const value_type& x, const value_type& y) const { return comp(x.first, y.first); } } public: explicit map(const Compare& comp = Compare(), const Allocator& = Allocator()); // ... };
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hi laserlight,
I do not understand why compare function object may throw exception. It contains no data members normally.
Here is how STL implements less in MSVC 2008. Any comments? If possible, could you show sample about how and when exception is thrown during construction or assignment for compare object please?
Code:// TEMPLATE STRUCT less template<class _Ty> struct less : public binary_function<_Ty, _Ty, bool> { // functor for operator< bool operator()(const _Ty& _Left, const _Ty& _Right) const { // apply operator< to operands return (_Left < _Right); } };
regards,
George
The keyword is "normally".I do not understand why compare function object may throw exception. It contains no data members normally.
I cannot think of a good example right now, but a rather contrived example is one where the Compare object queries a database during construction to find out how it should compare.Here is how STL implements less in MSVC 2008. Any comments? If possible, could you show sample about how and when exception is thrown during construction or assignment for compare object please?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The user of the map can supply his or her own compare object. The standard does not require that compare object not to throw in it's copy constructor or assignment operator, and the copy constructor or assignment operator may be used during the swap, so there is no way for the standard to guarantee that the swap won't throw.
Thanks laserlight and Daved,
Question answered.
[QUOTE=Daved;735079]The user of the map can supply his or her own compare object. The standard does not require that compare object not to throw in it's copy constructor or assignment operator, and the copy constructor or assignment operator may be used during the swap, so there is no way for the standard to guarantee that the swap won't throw.[/QUOT]
regards,
George