queue and exception

This is a discussion on queue and exception within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Just a basic question which I could not find answer from MSDN currently, for frequently used member functions ...

  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
    Portugal
    Posts
    7,383
    Use SGI for a more detailed knowledge of the standard library: http://www.sgi.com/tech/stl/
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,383
    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 08:04 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    20,974
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,892
    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,892
    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, 02:57 PM
  3. C++ Guided Design Programming Challenge
    By Cat in forum C++ Programming
    Replies: 44
    Last Post: 09-01-2003, 06:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21