[libcxx-commits] [PATCH] D122011: [libc++][ranges] Add implicit conversion to bool test for ranges::find{, if, if_not}

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 18 09:05:18 PDT 2022


ldionne added inline comments.


================
Comment at: libcxx/test/support/immobile_bool.h:1
+//===----------------------------------------------------------------------===//
+//
----------------
ldionne wrote:
> How about `BooleanTestable`, since this is roughly an archetype for being boolean-testable?
Actually, here's what I came up with after discussing with you offline and playing around with the patch:

```
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LIBCXX_TEST_SUPPORT_BOOLEAN_TESTABLE_H
#define LIBCXX_TEST_SUPPORT_BOOLEAN_TESTABLE_H

struct BooleanTestable {
  constexpr operator bool() {
    return value_;
  }

  constexpr BooleanTestable(bool value) : value_{value} {}
  BooleanTestable(const BooleanTestable&) = delete;
  BooleanTestable(BooleanTestable&&) = delete;

private:
  bool value_;
};

template <typename T>
struct StrictComparable {
  constexpr StrictComparable(T value) : value_(value) { }

  friend constexpr BooleanTestable operator==(StrictComparable const& a, StrictComparable const& b) {
    return BooleanTestable(a.value_ == b.value_);
  }
  friend constexpr BooleanTestable operator!=(StrictComparable const& a, StrictComparable const& b) {
    return BooleanTestable(a.value_ != b.value_);
  }

private:
  T value_;
};

#endif // LIBCXX_TEST_SUPPORT_BOOLEAN_TESTABLE_H
```

Then you can use it as:

```
{
    StrictComparable<int> a[] = {1, 2, 3, 4};
    auto ret = std::ranges::find(a, a + 4, StrictComparable<int>{4});
    assert(ret == a + 3);
}
{
    StrictComparable<int> a[] = {1, 2, 3, 4};
    auto ret = std::ranges::find(a, StrictComparable<int>{4});
    assert(ret == a + 3);
}
```

I'm not a big fan of the name `StrictComparable`, but I like that we're not mixing up the type in the range and the boolean testable archetype.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122011/new/

https://reviews.llvm.org/D122011



More information about the libcxx-commits mailing list