Skip to content

Commit 5a0cd7c

Browse files
committed
Simplify the to_vector polyfill to work with c++23 and c++20
1 parent e362cdb commit 5a0cd7c

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

include/podio/UserDataCollection.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ class UserDataCollection : public CollectionBase {
101101
///
102102
/// @returns a UserDataCollection populated with the values from the input
103103
/// range
104-
static UserDataCollection from(detail::RangeConvertibleTo<BasicType> auto&& range) {
104+
template <detail::RangeConvertibleTo<BasicType> R>
105+
static UserDataCollection from(R&& range) {
105106
UserDataCollection coll;
106-
coll._vec = range | detail::to_vector<BasicType>;
107+
coll._vec = detail::to_vector<BasicType>(std::forward<R>(range));
107108
return coll;
108109
}
109110

include/podio/utilities/TypeHelpers.h

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -250,46 +250,21 @@ namespace detail {
250250
concept RangeConvertibleTo = std::ranges::input_range<R> && std::convertible_to<std::ranges::range_value_t<R>, T>;
251251

252252
#if defined(__cpp_lib_ranges_to_container)
253-
template <typename T>
254-
auto to_vector() {
255-
return std::ranges::to<std::vector<T>>();
256-
}
257-
258253
template <typename T, std::ranges::input_range R>
259254
auto to_vector(R&& r) {
260255
return std::ranges::to<std::vector<T>>(std::forward<R>(r));
261256
}
262257
#else
263-
// Implement a small poly-fill that does the trick
264-
template <typename C>
265-
struct to_impl {
266-
template <std::ranges::input_range R>
267-
auto operator|(R&& range) const {
268-
using RangeType = std::ranges::range_reference_t<R>;
269-
using ValueType = typename C::value_type;
270-
static_assert(std::convertible_to<RangeType, ValueType>,
271-
"Input range elements must be convertible to the container value type");
272-
273-
C container;
274-
if constexpr (requires(C& c, std::size_t n) { c.reserve(n); } && std::ranges::sized_range<R>) {
275-
container.reserve(range.size());
276-
}
277-
std::ranges::copy(range, std::back_inserter(container));
278-
return container;
279-
}
280-
281-
auto operator()() const {
282-
return *this;
258+
// Implement a very simple polyfill to maintain compatibility with c++20
259+
template <typename T, std::ranges::input_range R>
260+
auto to_vector(R&& range) {
261+
std::vector<T> container;
262+
if constexpr (std::ranges::sized_range<R>) {
263+
container.reserve(std::ranges::size(range));
283264
}
284-
};
285-
286-
template <std::ranges::input_range R, typename C>
287-
auto operator|(R&& range, const to_impl<C>& adapter) {
288-
return adapter.operator|(std::forward<R>(range));
265+
std::ranges::copy(range, std::back_inserter(container));
266+
return container;
289267
}
290-
291-
template <typename T>
292-
inline constexpr to_impl<std::vector<T>> to_vector{};
293268
#endif
294269

295270
} // namespace detail

0 commit comments

Comments
 (0)