This is an archive of the discontinued LLVM Phabricator instance.

[libc++][P2321R2] Add vector<bool>::reference::operator=(bool) const
ClosedPublic

Authored by philnik on Jan 19 2022, 5:57 PM.

Details

Summary

Add vector<bool>::reference::operator(bool) const

Diff Detail

Event Timeline

philnik requested review of this revision.Jan 19 2022, 5:57 PM
philnik created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJan 19 2022, 5:57 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Quuxplusone requested changes to this revision.Jan 19 2022, 6:16 PM

(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.

This revision now requires changes to proceed.Jan 19 2022, 6:16 PM

(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.

(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?

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.

philnik updated this revision to Diff 404591.Jan 31 2022, 10:10 AM
  • Add test
Quuxplusone added inline comments.
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.

philnik added inline comments.Jan 31 2022, 10:47 AM
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?

ldionne added inline comments.Jan 31 2022, 12:09 PM
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.

philnik updated this revision to Diff 405431.Feb 2 2022, 1:50 PM
philnik marked 2 inline comments as done.
  • Address commments
ldionne accepted this revision.Feb 2 2022, 2:32 PM

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.

Quuxplusone accepted this revision.Feb 10 2022, 10:04 AM
This revision is now accepted and ready to land.Feb 10 2022, 10:04 AM