Skip to content
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

add const_view #346

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

add const_view #346

wants to merge 6 commits into from

Conversation

dvirtz
Copy link

@dvirtz dvirtz commented Oct 10, 2019

I plan to make a proposal out of this and would like to have an implementation looked at beforehand.

Note that the adapter is named views::as_const as opposed to const_ in range-v3. This is inspired by the fact that views::move is conceptually applying std::move on every element the same way that this view conceptually applies std::as_const.
Another possible name is const_elements, suggested by @cjdb.

Copy link
Collaborator

@cjdb cjdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looking good! There's a few things that need to be addressed on a technical level, as well as a few on a stylistic level.

I've done this review on my phone, so I'll do a more detailed one tomorrow when I can interact with GitHub a bit more nicely.

Thanks for putting this effort in 😄


constexpr auto base() const noexcept -> R { return base_; }

constexpr __iterator<false> begin()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to adda requirement for detail::simple_view on each of the non-const overloads.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant !simple_view.

constexpr auto size() const requires sized_range<R>
{
return __stl2::size(base_);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A non-const overload is necessary for size.


__iterator() = default;

constexpr explicit __iterator(parent_t &parent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/parent &/parent& /


constexpr void operator++(int)
{
current_++;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should delegate to ++*this

}
};

template <class R>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the requirements here too, as well as viewable_range.


namespace views::ext
{
struct __as_const_fn : detail::__pipeable<__as_const_fn>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inheritance not needed for this one. Also, please look at some of the other adaptors to see what's usually done. I recommend take or drop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's necessary for adaptors with no arguments. same as views::move.

template <input_range R>
requires view<R>
class const_view : public view_interface<const_view<R>>
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this

STL2_OPEN_NAMESPACE {
	namespace ext {
		template<input_range R>
		requires view<R>
		struct const_view : view_interface<const_view<R>> {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this project needs a .clang-format file

const_view() = default;
constexpr explicit const_view(R base) : base_(std::move(base)) {}

constexpr auto base() const noexcept -> R { return base_; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't use trailing return types.

constexpr __iterator<true> begin() const requires range<const R>
{
return __iterator<true>{*this, __stl2::begin(base_)};
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray semicolon. Also, I think @CaseyCarter's style is to have the opening brace on the same line as the function's head.

return __stl2::size(base_);
}

constexpr auto size() const requires sized_range<R>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/sized_range<R>/sized_range<const R>

}
else
{
return __stl2::end(self.base_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to make a custom sentinel type for this. @timsong-cpp pointed out that if a range adaptor has its own iterator, it needs its own sentinel so that we can compare both the adaptor's iterator and the adaptee's iterator against the sentinel.

include/stl2/view/const.hpp Outdated Show resolved Hide resolved
include/stl2/view/const.hpp Outdated Show resolved Hide resolved
include/stl2/view/const.hpp Outdated Show resolved Hide resolved
include/stl2/view/const.hpp Outdated Show resolved Hide resolved
};

template <input_range R>
const_view(R&& r)->const_view<all_view<R>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also move this between the range adaptor and its iterator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants