-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
single_view should not copy the value #817
Comments
I disagree. Many views own state. They must have O(1) copy and move, and |
But this places unnecessary requirements on the element type (one can't use it with non-copiable types). It is also a clear performance pessimization (copy can be expensive). If not changed, it should probably be renamed, since in the current form it is more of a container, rather than a view. Use case I had is: I have a generic function that takes a range of objects and processes them. But in one place I want to call this function with only one object. I considered passing a |
Additionally, there is the following text in the Ranges TS:
Which implies that the range that owns its elements should not be considered a view. |
That's merely a heuristic for guessing at the view-ness of a range. That heuristic cannot know that the single_view only holds one element. |
@ilyapopov Assuming this is a requirement in your work, as a work around, how about #include <type_traits>
#include <range/v3/view/single.hpp>
#include <range/v3/view/indirect.hpp>
struct reference_single_view_fn {
template<typename T, std::enable_if_t<std::is_lvalue_reference<T>::value, bool> = true>
auto operator(T&& value) const {
return ranges::view::single(&value) | ranges::view::indirect;
}
};
static constexpr reference_single_view_fn reference_single_view{}; |
@CaseyCarter can you remind me why the |
No idea. template<CopyConstructible T>
requires is_object_v<T>
class single_view : public view_interface<single_view<T>> {
private:
semiregular<T> value_; // exposition only
public:
single_view() = default;
constexpr explicit single_view(const T& t);
constexpr explicit single_view(T&& t);
template<class... Args>
requires Constructible<T, Args...>
constexpr single_view(in_place_t, Args&&... args);
constexpr T* begin() noexcept;
constexpr const T* begin() const noexcept;
constexpr T* end() noexcept;
constexpr const T* end() const noexcept;
constexpr static ptrdiff_t size() noexcept;
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
};
template<class T>
explicit single_view(T&&) -> single_view<decay_t<T>>; It's roughly |
I like P0896's formulation better. |
FWIW: |
#918 makes |
Currently,
single_view
requires value type to be copy constructible, and copies it into itself. This, in my view, goes against the concept of view, which is not supposed to own values, but to refer to the original values. Thus I believesingle_view
should store a reference to original value.The text was updated successfully, but these errors were encountered: