struct A {
int* a_ptr;
};
struct B {
A* b_ptr;
};
struct C { // iterator
B* c_ptr;
//...
int& f() noexcept(?) { // provides some access if 'C' is valid
return c_ptr->b_ptr->a_ptr;
}
};
Imagine: C is some custom iterator, which is always valid until its container is deleted. Otherwise, calling C::f will result in UB. We trust the user that he will never try to create C, to delete its container and then to call C::f (we will warn him that it is a bad practice).
So, the question is whether I can put noexcept(true) on f or not from the code-writing-ethics point of view? I am looking for some reasoning for any of the decisions.
So, if a function never throws exceptions it is appropriate to put on
noexcept(true)even if it can invokeUBunder incorrect usage.However... (check commentaries)