Skip to content

Commit 808f5d1

Browse files
[ADT] Simplify a constructor of iterator_range (NFC) (#158005)
Without this patch, we determine whether one iterator type can be converted to another in a roundabout way. Specifically, explicitly_convertible uses std::void_t to determine whether the given conversion expression is well formed, yielding std::true_type/std::false_type. Then the boolean value is passed to std::enable_if_t to obtain void again. That is, we are doing a roundtrip from void to a boolean value and back. This patch removes the roundtrip by directly using std::void_t inside the constructor's template parameter list. Now, explicitly_converted_t is very similar to std::is_constructible, but there a couple of corner cases where they evaluate to different values. For now, this patch sticks to the same expression decltype(static_cast<To>(...)) to be safe.
1 parent 648831b commit 808f5d1

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

llvm/include/llvm/ADT/iterator_range.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@
2424

2525
namespace llvm {
2626

27-
template <typename From, typename To, typename = void>
28-
struct explicitly_convertible : std::false_type {};
29-
30-
template <typename From, typename To>
31-
struct explicitly_convertible<
32-
From, To,
33-
std::void_t<decltype(static_cast<To>(
34-
std::declval<std::add_rvalue_reference_t<From>>()))>> : std::true_type {
35-
};
36-
3727
/// A range adaptor for a pair of iterators.
3828
///
3929
/// This just wraps two iterators into a range-compatible interface. Nothing
@@ -42,17 +32,20 @@ template <typename IteratorT>
4232
class iterator_range {
4333
IteratorT begin_iterator, end_iterator;
4434

35+
template <typename From, typename To>
36+
using explicitly_converted_t = decltype(static_cast<To>(
37+
std::declval<std::add_rvalue_reference_t<From>>()));
38+
4539
public:
4640
#if defined(__GNUC__) && \
4741
(__GNUC__ == 7 || (__GNUC__ == 8 && __GNUC_MINOR__ < 4))
4842
// Be careful no to break gcc-7 and gcc-8 < 8.4 on the mlir target.
4943
// See https://github.com/llvm/llvm-project/issues/63843
5044
template <typename Container>
5145
#else
52-
template <
53-
typename Container,
54-
std::enable_if_t<explicitly_convertible<
55-
llvm::detail::IterOfRange<Container>, IteratorT>::value> * = nullptr>
46+
template <typename Container,
47+
std::void_t<explicitly_converted_t<
48+
llvm::detail::IterOfRange<Container>, IteratorT>> * = nullptr>
5649
#endif
5750
iterator_range(Container &&c)
5851
: begin_iterator(adl_begin(c)), end_iterator(adl_end(c)) {

0 commit comments

Comments
 (0)