[libcxx-commits] [libcxx] [libc++] Use static_asserts for span::front() and span::back() when possible (PR #119381)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 10 08:00:50 PST 2024


ldionne wrote:

> Is this allowed? You could have code like
> 
> ```
> if (!span.empty()) { span.front(); }
> ```
> 
> which doesn't violate the precondition, but would be rejected with this patch.

Right, that's actually so obvious if you think about it. I was led down that path because `std::span::subspan<n>()` uses a `static_assert`, but that's because the input is a constant so it's OK to make it ill-formed in that case. That reasoning doesn't apply here.

It's still a shame that we can't guarantee a diagnostic at compile-time. I tried thinking of ways to make that work with `__builtin_constant_p` but I can't get to anything that works since the `static_assert` always triggers when the function is instantiated.

https://github.com/llvm/llvm-project/pull/119381


More information about the libcxx-commits mailing list