Thread: queue and exception

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

    queue and exception

    Hello everyone,


    Just a basic question which I could not find answer from MSDN currently, for frequently used member functions for queue class, there is,

    1. push;
    2. pop;
    3. back;
    4. front.

    I know (1) will throw exception, for example, out of memory. How about (2), (3) and (4)? Will they be possible to throw any exceptions and in what situations if there are exceptions? From MSDN declaration of exception specification for the functions, I can not find the answer.


    thanks in advance,
    George

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Use SGI for a more detailed knowledge of the standard library: http://www.sgi.com/tech/stl/
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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


    I read through, but no answer about whether exception will be thrown or not in the functions I listed in my question.

    http://www.sgi.com/tech/stl/queue.html

    Quote Originally Posted by Mario F. View Post
    Use SGI for a more detailed knowledge of the standard library: http://www.sgi.com/tech/stl/

    regards,
    George

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    If it doesn't say anything...

    ... you got it.

    EDIT: Went there. Take a look when it mentions preconditions for the members. Usually preconditions are handled by exceptions in the STL.
    Last edited by Mario F.; 02-20-2008 at 09:04 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The member functions pop(), back() and front() of std::queue will not throw any exceptions, but attempting to use them on an empty queue results in undefined behaviour.
    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

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


    Any push throws exception when there is bad_alloc? Are there any other situations?

    Quote Originally Posted by laserlight View Post
    The member functions pop(), back() and front() of std::queue will not throw any exceptions, but attempting to use them on an empty queue results in undefined behaviour.

    Hi Mario,


    I searched the link you referred for "preconditions", but can not find related exception information. It is appreciated if you could point out.

    Quote Originally Posted by Mario F. View Post
    If it doesn't say anything...

    ... you got it.

    EDIT: Went there. Take a look when it mentions preconditions for the members. Usually preconditions are handled by exceptions in the STL.

    regards,
    George

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Any push throws exception when there is bad_alloc? Are there any other situations?
    The copy constructor of the object pushed might throw.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    The root cause of the exception of copy constructor should be bad_alloc? Any other possibilities? User defined exception?

    Quote Originally Posted by CornedBee View Post
    The copy constructor of the object pushed might throw.

    regards,
    George

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    User-defined. It depends on the type. A std::string might throw a bad_alloc if it can't allocate memory for the new string. Other classes might to something else.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    My question is answered.

    Quote Originally Posted by CornedBee View Post
    User-defined. It depends on the type. A std::string might throw a bad_alloc if it can't allocate memory for the new string. Other classes might to something else.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what are these errors?
    By JJH35 in forum C++ Programming
    Replies: 1
    Last Post: 05-22-2007, 11:24 PM
  2. linked-list queue
    By the_winky_files in forum C Programming
    Replies: 17
    Last Post: 11-21-2005, 03:57 PM
  3. C++ Guided Design Programming Challenge
    By Cat in forum C++ Programming
    Replies: 44
    Last Post: 09-01-2003, 06:41 PM