pitrou commented on code in PR #36334: URL: https://github.com/apache/arrow/pull/36334#discussion_r1251173470
########## cpp/src/arrow/util/span.h: ########## @@ -0,0 +1,129 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> +#include <iterator> +#include <type_traits> + +namespace arrow::util { + +/// std::span polyfill. +/// +/// Does not support static extents. +template <typename T> +class span { + static_assert(sizeof(T), + R"( +std::span allows contiguous_iterators instead of just pointers, the enforcement +of which requires T to be a complete type. arrow::util::span does not support +contiguous_iterators, but T is still required to be a complete type to prevent +writing code which would break when it is replaced by std::span.)"); + + public: + using element_type = T; + using value_type = std::remove_cv_t<T>; + using iterator = T*; + using const_iterator = T const*; + + span() = default; + span(const span&) = default; + span& operator=(const span&) = default; + + template <typename M, typename = std::enable_if_t<std::is_same_v<T, M const>>> + // NOLINTNEXTLINE runtime/explicit + constexpr span(span<M> mut) : span{mut.data(), mut.size()} {} + + constexpr span(T* data, size_t count) : data_{data}, size_{count} {} + + constexpr span(T* begin, T* end) + : data_{begin}, size_{static_cast<size_t>(end - begin)} {} + + template < + typename R, + typename DisableUnlessConstructibleFromDataAndSize = + decltype(span<T>(std::data(std::declval<R&>()), std::size(std::declval<R&>()))), + typename DisableUnlessSimilarTypes = std::enable_if_t<std::is_same_v< + std::decay_t<std::remove_pointer_t<decltype(std::data(std::declval<R&>()))>>, + std::decay_t<T>>>> + // NOLINTNEXTLINE runtime/explicit, non-const reference + constexpr span(R& range) : span{std::data(range), std::size(range)} {} Review Comment: Should this be `R&&`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
