On 15.04.19 09:06, Emil Dotchevski via Boost wrote:
On Sun, Apr 14, 2019 at 11:57 PM Rainer Deyke via Boost < boost@lists.boost.org> wrote:
No. A function is not required to check its invariants and preconditions. If a function is defined as taking a non-empty variant, then it is up to the caller to make sure the variant is not empty before passing it to the function.
The point is, there will be checks, in various functions (e.g. in "the caller"),
No checks are necessary, unless a variants passes from a context where an empty state is allowed to one where it isn't. If the empty state can only be entered via exception, then the only context in which an empty state can exist is in the aftermath of an exception, where we would have to either replace the empty variant with a non-empty variant or allow the variant to leave the scope. But we would have to do this even with a never-empty variant in order to maintain our invariants, so the actual code would be no different in either case.
except if we know the state is impossible, A.K.A. the never-empty guarantee.
We can know that the state is impossible even if the guarantee is not an
intrinsic property of the variant type.
variant