Skip to content

Commit fc01a55

Browse files
[Support] Refactor IsResizableBase with "constexpr if" (NFC) (llvm#157326)
We have two implementations of IsResizableBase that are selected with a boolean template parameter. This patch consolidates them into one with "constexpr if". The "constexpr if" condition uses llvm::is_detected to check the availability of resize().
1 parent 7ef9530 commit fc01a55

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,39 +1850,29 @@ template <> struct IsFlowSequenceBase<true> {
18501850
static const bool flow = true;
18511851
};
18521852

1853-
template <typename T, typename U = void>
1854-
struct IsResizable : std::false_type {};
1855-
18561853
template <typename T>
1857-
struct IsResizable<T, std::void_t<decltype(std::declval<T>().resize(0))>>
1858-
: public std::true_type {};
1859-
1860-
template <typename T, bool B> struct IsResizableBase {
1861-
using type = typename T::value_type;
1862-
1863-
static type &element(IO &io, T &seq, size_t index) {
1864-
if (index >= seq.size())
1865-
seq.resize(index + 1);
1866-
return seq[index];
1867-
}
1868-
};
1854+
using check_resize_t = decltype(std::declval<T>().resize(0));
18691855

1870-
template <typename T> struct IsResizableBase<T, false> {
1856+
template <typename T> struct IsResizableBase {
18711857
using type = typename T::value_type;
18721858

18731859
static type &element(IO &io, T &seq, size_t index) {
1874-
if (index >= seq.size()) {
1875-
io.setError(Twine("value sequence extends beyond static size (") +
1876-
Twine(seq.size()) + ")");
1877-
return seq[0];
1860+
if constexpr (is_detected<check_resize_t, T>::value) {
1861+
if (index >= seq.size())
1862+
seq.resize(index + 1);
1863+
} else {
1864+
if (index >= seq.size()) {
1865+
io.setError(Twine("value sequence extends beyond static size (") +
1866+
Twine(seq.size()) + ")");
1867+
return seq[0];
1868+
}
18781869
}
18791870
return seq[index];
18801871
}
18811872
};
18821873

18831874
template <typename T, bool Flow>
1884-
struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>,
1885-
IsResizableBase<T, IsResizable<T>::value> {
1875+
struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>, IsResizableBase<T> {
18861876
static size_t size(IO &io, T &seq) { return seq.size(); }
18871877
};
18881878

0 commit comments

Comments
 (0)