Posted Apr 2, 2012 14:34 UTC (Mon) by jwakely
In reply to: Uses
Parent article: Go version 1 released
C++ already HAS a way of declaring that a function won't throw-- add "throw()" to the end of the function. They're going to add another way?
A non-empty exception spec i.e. throw(X,Y,Z)
is deprecated, but you're left with (as before) throw()
meaning the function won't throw or if that's not given then (as before) it means the function might throw, and could throw any type.
But in order to make throw() useful for metaprogramming there's a new way to spell it which is noexcept(true) or simply noexcept. You can also say noexcept(false) to say a function might throw, which seems useless until you realise the boolean can be any constant expression, so a function template with parameter T can be declared noexcept(is_nothrow_copyable<T>::value) i.e. the function guarantees not to throw if copying T won't throw. There is also a noexcept operator that can be used to query whether arbitrary expressions will throw.
It's not a new mechanism, it's the same one with the useless part (non-empty exception specs) removed, and more useful syntax.
If library code isn't updated to the new mechanism it doesn't matter, it still works fine (but noone should be using non-empty exception specs anyway, they're crap) and throw() is equivalent to noexcept.
to post comments)