Thread: swap function of STL container

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    swap function of STL container

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The 2003 edition of C++ Standard states:
    Quote Originally Posted by Section 23.1
    no swap() function throws an exception unless that exception is thrown by the copy constructor or assignment operator of the container’s Compare object
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi laserlight,


    I am confused about what means "copy constructor or assignment operator of the container’s Compare object"?

    1. Compare operator will utilize copy constructor?

    2. Compare operator will utilize assignment operator?

    Quote Originally Posted by laserlight View Post
    The 2003 edition of C++ Standard states:

    regards,
    George

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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.

    Quote Originally Posted by laserlight View Post
    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.

    regards,
    George

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    You have to swap the Compare objects too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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?

    Quote Originally Posted by laserlight View Post
    You have to swap the Compare objects too.

    regards,
    George

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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)?
    Take for example std::map:
    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());
        // ...
    };
    The Compare object is just some function object that is used to compare the keys of the map such that they can be ordered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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);
    		}
    	};

    Quote Originally Posted by laserlight View Post
    Take for example std::map:
    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());
        // ...
    };
    The Compare object is just some function object that is used to compare the keys of the map such that they can be ordered.

    regards,
    George

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not understand why compare function object may throw exception. It contains no data members normally.
    The keyword is "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?
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Double Link List Swap function
    By kennny2004 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 11:52 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM