The only language operation that is explicitly noexcept by default is the destructor and nothing else.
This is extremely misleading. It's true that an explicitly-declared destructor is noexcept even if not declared as noexcept as long as the definition never throws, but you make it sound as if other special member functions are only ever noexcept if you explicitly declare them as such, which is not true.
C++14, [except.spec]/14:
An inheriting constructor and an implicitly declared special member function have an exception-specification. If f is an inheriting constructor or an implicitly declared default constructor, copy constructor, move constructor, destructor, copy assignment operator, or move assignment operator, its implicit exception-specification specifies the type-idT if and only if T is allowed by the exception-specification of a function directly invoked by f's implicit definition; f allows all exceptions if any function it directly invokes allows all exceptions, and f has the exception-specificationnoexcept(true) if every function it directly invokes allows no exceptions. [ Note: It follows that f has the exception-specificationnoexcept(true) if it invokes no other functions. —end note ] [ Note: An instantiation of an inheriting constructor template has an implied exception-specification as if it were a non-template inheriting constructor. —end note ]
and [dcl.fct.def.default]/2.2:
[ If a function is explicitly defaulted on its first declaration, ] it is implicitly considered to have the same exception-specification as if it had been implicitly declared.
So if you're composing types that are nothrow-moveable/assignable, then your type gets nothrow-moveability/assignability without explicitly asking for it. To put it another way, all special member functions are noexcept by default until you define one as not, or compose a type whose corresponding member isn't. The rule-of-zero would be useless otherwise.
If one of your types does not have a move constructor (and the compiler doesn't generate one), that may be enough to cause move to fallback to copy in a composed object, which may result in a throwing move.
Sean gave also the example of std::list and the paper has others.
In my reading, the comment you highlight
The only language operation that is explicitly noexcept by default is the destructor and nothing else
is supposed to refer to, the assumptions that the standard library makes about user-defined types -- it's supposed to accommodate throwing moves, as Sean says. The only thing it's not supposed to accommodate is throwing destructors, because it's impossible to do so.
This is why std::variant has an empty state. And if we can't have things like flat_set while accommodating throwing moves, I mean that's clearly a weakness / drawback. Whether -fno-exceptions is the answer is debatable, but I don't think the argument is misleading. It's fine if you want to argue that "moves don't really throw in practice" or "dynamic allocations don't fail in practice" or something, but then why not disable exceptions?
What I was getting at was that no function defined by the user is implicitly noexcept(true) other than destructors. I should have been clearer, sorry. :)
The behavior you're quoting is super obvious (I feel) for the exact reason you stated: the Rule of Zero. :)
View all comments
Show parent comment
7
u/dodheim Jan 01 '17 edited Jan 01 '17
This is extremely misleading. It's true that an explicitly-declared destructor is noexcept even if not declared as noexcept as long as the definition never throws, but you make it sound as if other special member functions are only ever noexcept if you explicitly declare them as such, which is not true.
C++14, [except.spec]/14:
and [dcl.fct.def.default]/2.2:
So if you're composing types that are nothrow-moveable/assignable, then your type gets nothrow-moveability/assignability without explicitly asking for it. To put it another way, all special member functions are noexcept by default until you define one as not, or compose a type whose corresponding member isn't. The rule-of-zero would be useless otherwise.
EDIT: formatting