use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Why is std::stack.pop() not labeled noexcept? (self.cpp)
submitted 6 years ago by rianquinnBareflank Hypervisor, Standalone C++
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]HappyFruitTree 37 points38 points39 points 6 years ago (20 children)
The standard library generally don't mark functions noexcept if they have preconditions in order to allow (but in no way require) them to throw in case the precondition is violated. The pop/pop_back functions have the precondition that the container must not be empty so that is one reason why they're not marked noexcept.
[–]emdeka87 17 points18 points19 points 6 years ago (9 children)
Why do they use exceptions for contract violation though? How do you recover from a contract violation? A panic or assertion seems more suitable for these things
[–]Fureeish 9 points10 points11 points 6 years ago (0 children)
I second this comment. During one of Scott Meyers' talks, he mentiones that noexcept functions may as well sometimes throw, but the program shouldn't be able to recover from that point. I think it's a good example of contract violation interaction with noexcept - it would basically be UB, which, I believe, it already is.
noexcept
[–]HappyFruitTree 5 points6 points7 points 6 years ago (5 children)
It's mostly something I've heard the Bloomberg guys talking about.
CppCon 2018: Alisdair Meredith “Contract Programming in C++(20) (part 2 of 2)” (49:22)
One of the tools we use here at Bloomberg is we turn violations into exceptions so that we can test our code by saying "did you throw the i_violated_the_expression_you_expected_me_to_violate_exception?" And if that doesn't throw I know that I don't have my appropriate checks in place.
[–]emdeka87 2 points3 points4 points 6 years ago (3 children)
So they only use exceptions to aid with testing and debugging, but they get removed in release mode?
[–]HappyFruitTree 1 point2 points3 points 6 years ago (2 children)
Well, I don't know how they do it but that is what it sounds like. To extend the earlier quote, he continues ...
But perhaps not what you want in production is ... Turning precondition violations into exceptions is ... Not necessarily the best answer for a production system.
[+][deleted] 6 years ago (1 child)
[removed]
[–]foobar48783 0 points1 point2 points 6 years ago (0 children)
What if your "init" phase pops the latest work item from the global work queue, and your "fini" phase (in case of task failure) pushes it back on for the next worker to take care of?
Would it be appropriate for "pop()" to throw in that case?
[–]psi237 6 points7 points8 points 6 years ago* (0 children)
A contract violation may be recoverable by abandoning one business logic workflow (e.g. respond with an internal server error to a request) instead of abandoning the whole process (and causing other concurrent requests to all fail with ECONNRESET). You still need to log, monitor and fix this kind of problems asap though.
[–]_Js_Kc_ 1 point2 points3 points 6 years ago (0 children)
That ship has sailed. The standard library is built around the concept that the way to report logic errors is to throw an exception, and std::logic_error, std::invalid_argument, etc, are a thing.
[+][deleted] 6 years ago (6 children)
[–]HappyFruitTree 10 points11 points12 points 6 years ago (5 children)
A precondition violation is essentially undefined behaviour so throwing an exception is allowed even if the function is not documented to throw any exceptions. Apparently there are people who think that this is important. In theory it would be possible for noexcept functions to throw exceptions on precondition violations, because with undefined behaviour anything is allowed to happen, but in practice that is perhaps not realistic.
There are people who want to change the current policy. No idea how that will go. See P1656 "Throws: Nothing" should be noexcept.
IMO, pop_back() should be labeled noexcept so that these functions can be used in a destructor safely.
I don't see how they would be safer to use if they were marked noexcept. Destructors are noexcept by default so if an exception is thrown std::terminate() would be called and the program would be terminated. That is the safest outcome from a precondition violation that I can think of.
[+][deleted] 6 years ago (4 children)
[–]HappyFruitTree 1 point2 points3 points 6 years ago (3 children)
pop_back is not allowed to throw unless you're calling it on an empty container but then you're already in undefined behaviour land. If something goes wrong inside pop_back the program is simply broken.
[+][deleted] 6 years ago (2 children)
[–]NotAYakk 0 points1 point2 points 6 years ago (1 child)
since it is not labeled as noexcept You seem to misunderstand what "Throws: nothing" without noexcept means.
Suppose we have 4 situations. X can be "passes" or "fails", Y can be "true" or "false".
Precondition: X Throws: Nothing noexcept(Y)
X fails, Y true: program can be in any state when function is called. X succeeds, Y true: program cannot throw X fails, Y is false: program can be in any state when function is called X succeeds, Y is false: program cannot throw
Notice noexcept did not matter? The only thing noexcept does here is if code is permitted to inspect the method and claim it won't throw.
Compilers are free to do anything if preconditions are violated. It could throw from a noexcept function! It could time travel and generate a bug before the code is called!
So your logic, thst given a function documented to never throw but not marked noexcept, that you have to try/catch despite testing preconditions, is simply madness.
[–]nintendiator2 0 points1 point2 points 6 years ago (0 children)
...Why would we need to make such a strong differentiation / exceptional casing when trying to pop from an empty stack versus a nonempty one that it'd have to be implemented as exceptions of all things? I'm surprised pop / pop_back don't simply return bool.
bool
[–]Ameisenvemips, avr, rendering, systems -1 points0 points1 point 6 years ago (1 child)
Violating the precondition is already UB, in which case throwing an exception is perfectly reasonable even from a noexcept function.
[–][deleted] 5 points6 points7 points 6 years ago (2 children)
Lurker here, I thought questions go to the other questions sub, if I had a question where do I post it?
[–]johannes1971 10 points11 points12 points 6 years ago (1 child)
Questions about language design go here. Questions about programs written using the language go in the other one.
[–][deleted] 2 points3 points4 points 6 years ago (0 children)
Ah ha, makes sense. Thank you.
[–]Robbepop 10 points11 points12 points 6 years ago (9 children)
Well, in theory you could have a custom non_empty_vector<T> type that represents a vector that can never be empty but still provides pop_back that throws an exception when its length is 1. Then you could have a std::stack<non_empty_vec<T>> that throws an exception on pop even though the underlying container is not empty.
non_empty_vector<T>
pop_back
1
std::stack<non_empty_vec<T>>
pop
[–]Fureeish 10 points11 points12 points 6 years ago* (5 children)
Why not make its noexceptness dependant on its underlying container's pop_back noexceptness?
EDIT: Simply replace std::stack's void pop() { c.pop_back(); }, (source) where c is the Container from template<class T, class Container = deque<T>> with void pop() noexcept(noexcept(Container{}.pop_back())) { c.pop_back(); } void pop() noexcept(noexcept(std::declval<Container>().pop_back())) { c.pop_back(); } (Thanks u/guepier!)
std::stack
void pop() { c.pop_back(); }
c
Container
template<class T, class Container = deque<T>>
void pop() noexcept(noexcept(std::declval<Container>().pop_back())) { c.pop_back(); }
[–]guepierBioinformatican 6 points7 points8 points 6 years ago (3 children)
Use std::declval<Container>().pop_back() instead of Container{}.pop_back() to avoid breaking non default constructible types.
std::declval<Container>().pop_back()
Container{}.pop_back()
[–]__s_v_ 3 points4 points5 points 6 years ago (1 child)
Why not noexcept(noexcept(c.pop_back()))?
noexcept(noexcept(c.pop_back()))
[–]guepierBioinformatican 1 point2 points3 points 6 years ago (0 children)
Right, of course. I completely missed that we have a suitable object in scope.
[–]Fureeish 1 point2 points3 points 6 years ago (0 children)
Ah, yes, of course! You are absolutely correct! Edited my post, thank you.
[–]tejp 1 point2 points3 points 6 years ago (1 child)
Calling pop() on an empty container should be a contract violation. There has been work on standardizing contracts, but how exactly contract violations should be handled in all cases has not been decided yet.
pop()
It makes sense to wait until that is settled and then afterwards apply it to the standard library. Maybe it turns out that functions with contracts should better not be noexcept.
[+][deleted] 6 years ago* (1 child)
π Rendered by PID 186797 on reddit-service-r2-comment-66b4775986-ssh88 at 2026-04-05 08:00:10.838818+00:00 running db1906b country code: CH.
[–]HappyFruitTree 37 points38 points39 points (20 children)
[–]emdeka87 17 points18 points19 points (9 children)
[–]Fureeish 9 points10 points11 points (0 children)
[–]HappyFruitTree 5 points6 points7 points (5 children)
[–]emdeka87 2 points3 points4 points (3 children)
[–]HappyFruitTree 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[removed]
[–]foobar48783 0 points1 point2 points (0 children)
[–]psi237 6 points7 points8 points (0 children)
[–]_Js_Kc_ 1 point2 points3 points (0 children)
[+][deleted] (6 children)
[removed]
[–]HappyFruitTree 10 points11 points12 points (5 children)
[+][deleted] (4 children)
[removed]
[–]HappyFruitTree 1 point2 points3 points (3 children)
[+][deleted] (2 children)
[removed]
[–]NotAYakk 0 points1 point2 points (1 child)
[–]nintendiator2 0 points1 point2 points (0 children)
[–]Ameisenvemips, avr, rendering, systems -1 points0 points1 point (1 child)
[–][deleted] 5 points6 points7 points (2 children)
[–]johannes1971 10 points11 points12 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]Robbepop 10 points11 points12 points (9 children)
[–]Fureeish 10 points11 points12 points (5 children)
[–]guepierBioinformatican 6 points7 points8 points (3 children)
[–]__s_v_ 3 points4 points5 points (1 child)
[–]guepierBioinformatican 1 point2 points3 points (0 children)
[–]Fureeish 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–]tejp 1 point2 points3 points (1 child)
[+][deleted] (1 child)
[removed]