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.
C++ is built on the assumption that undefined behavior will never occur
There are no degrees of undefined behavior
noexcept
is about exceptions that your code throws, withthrow my_exception
So, if a function never throws exceptions it is appropriate to put on noexcept(true)
even if it can invoke UB
under incorrect usage.
However... (check commentaries)