Add vector<bool>::reference::operator(bool) const
Details
- Reviewers
• Quuxplusone Mordante ldionne - Group Reviewers
Restricted Project - Commits
- rGfb9646ed78a0: [libc++][P2321R2] Add vector<bool>::reference::operator=(bool) const
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
(1) The code itself looks great. I'd like to see a test case for this: both testing is_assignable_v, and testing its runtime behavior.
(2) You're missing the constexpr. But I know that's because we don't support constexpr vector yet. But but, there's no reason we couldn't constexpr __bit_reference itself right now today! Any appetite to do that soon, in a separate PR?
(3) Just for curious readers... I asked on Slack: Why is C++23 adding only one const overload of operator=, when there are two signatures already? Shouldn't there be a const __bit_reference& operator=(const __bit_reference& __x) const noexcept as well? If not, why not? (I don't see this discussed in [p2321].) Peter Dimov answers with an educated guess: The existing operator=(const __bit_reference&) is only there to block the defaulted copy-assignment operator. We don't need to block operator=(const __bit_reference&) const because it doesn't get implicitly generated. That makes enough sense for me!
libcxx/include/__bit_reference | ||
---|---|---|
69 | Nit: Consider matching the brace style of lines 58–60. |
(1) I couldn't find any tests for vector<bool>::reference. Could you tell me where they are or if there are any? If not, where should I put them?
(2) I can do that.
git grep flip libcxx/test/ | grep -v utilities/template.bitset — no hits. So I think we don't have any tests!
They should definitely go somewhere under libcxx/test/std/containers/sequences/vector.bool/; I would think directly in that directory with hierarchical filenames like reference.flip.pass.cpp, reference.assign.pass.cpp (or maybe reference.{const_,}assign_{bool,reference}.pass.cpp, one per overload?), etc. Although I wouldn't see anything wrong with reference/flip.pass.cpp, reference/assign.pass.cpp, etc either.
libcxx/test/std/containers/sequences/vector.bool/reference/assign_bool.pass.cpp | ||
---|---|---|
40 | You'll need to either hide this under TEST_STD_VER > 20 or remove the _LIBCPP_STD_VER > 20 from <__bit_reference>. Honestly, I think removing it from <__bit_reference> might be the better way to go, for consistency's sake, but I'm prepared for that to be controversial. @ldionne thoughts? can we make vector<bool>::iterator indirectly writable in C++20/17/14/11/03 as well as 2b? | |
46 | It might be a good idea to add a more motivating test case in addition to this simple one. I'm thinking something like https://godbolt.org/z/T1f3rzejs , although it's still not crazy good as an example. I wonder if @BRevzin knows a better motivating example off the top of his head. template<class T> void test(T t) { std::vector<T> v = {t}; const typename std::vector<T>::reference r = v[0]; // const-qualifying a reference type is a no-op r = t; // therefore this should work consistently for T=int and T=bool } ...Aha, you should also update std/containers/sequences/vector.bool/iterator_concept_conformance.compile.pass.cpp because this should now be true, not false! static_assert(std::indirectly_writable<std::vector<bool>::iterator, bool>); That might be a sufficiently "motivating" example. |
libcxx/test/std/containers/sequences/vector.bool/reference/assign_bool.pass.cpp | ||
---|---|---|
46 | Did you miss the iterator_concept_conformance.compile.pass.cpp diff? Is that enough? |
libcxx/test/std/containers/sequences/vector.bool/reference/assign_bool.pass.cpp | ||
---|---|---|
40 | I would guard this with TEST_STD_VER since this is so easy to do, and that way we'll be more strictly conforming. |
I know it's pretty lame that this is inconsistent between C++20 and pre C++20, but I think this is the correct thing to do since we strive to remain strictly standards conforming.
Nit: Consider matching the brace style of lines 58–60.