[libcxx-commits] [PATCH] D149686: [libc++][DISCUSSION] Exploring PSTL backend customization points

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 9 10:53:40 PDT 2023


ldionne added inline comments.


================
Comment at: libcxx/include/__algorithm/pstl_for_each.h:52
+  using _Backend = typename __select_backend<_RawPolicy>::type;
+  if constexpr (requires {std::__pstl_for_each_n(_Backend{}, __first, __size, __func); }) {
+    __pstl_for_each_n<_RawPolicy>(_Backend{}, std::move(__first), __size, std::move(__func));
----------------
We could do this in C++17:

```
auto __for_each_n_test = [](auto&& ...args) -> void_t<decltype(std::__pstl_for_each_n<_RawPolicy>(args...))> {};
if constexpr (__is_valid(__for_each_n_test, _Backend{}, __first, __size, __func)) {
  // ...
} else {
  // ...
}
```

Where:

```
template <typename _Func, typename ..._Args, typename = decltype(
  std::declval<_Func&&>()(std::declval<_Args&&>()...)
)>
constexpr bool __is_valid_impl(int) { return true; }

template <typename _Func, typename ..._Args>
constexpr bool __is_valid_impl(...) { return false; }

template <typename _Func, typename ..._Args>
constexpr bool __is_valid(_Func&&, _Args&& ...) {
  return __is_valid_impl<_Func&&, _Args&&...>(int{});
}
```

You might run into issues with `__is_valid(__for_each_n_test, _Backend{}, __first, __size, __func)` not being a constant expression because you are passing references to function arguments. Not sure if it'll be a problem cause you never actually read them. But if it is, then you can switch to returning `-> auto std::true_c{}` and `-> auto std::false_c{}` from your `__is_valid` function, and then you call it like:

```
if constexpr (decltype(__is_valid(as-before)){}) {
  // ...
}
```

OK, not quite as nice, but it works around the constexpr issue.

If you don't like this, you can also try to pass the argument types directly instead of the arguments themselves, like `__is_valid<decltype(__for_each_n_test), args...>()`. I'm not sure I quite like this, but it's an option on the table.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149686



More information about the libcxx-commits mailing list