std::views ?

71 views
Skip to first unread message

Stefan Zager

unread,
Mar 16, 2026, 7:35:01 PMMar 16
to c...@chromium.org
I don't pay close attention to the adoption of newer C++ features, but I just ran up against this one.

Sure would be nice for what I'm doing if I could use std::ranges::transform_view.

Victor Vianna

unread,
Mar 17, 2026, 3:16:32 AMMar 17
to cxx, Stefan Zager
They are banned here and the docs link to the thread with the rationale, in case you want to revisit any of the arguments there.

Stefan Zager

unread,
Mar 17, 2026, 1:54:37 PMMar 17
to Victor Vianna, cxx
I'm trying to replace this very succinct use of views (which compiles and passes tests):

```
  auto LayersThatShouldPushProperties() const {
    return std::views::transform(layers_that_should_push_properties_,
                                 [this](int id) { return find(id)->get(); });
```

... with a custom iterator class, but hitting a compiler error I can't quite figure, and I'd appreciate any available help. Here's the code:

```
  class DirtyLayerIterator {
   public:
    DirtyLayerIterator(const OwnedLayerImplList& layer_list,
                       SetType::const_iterator cur)
        : layer_list_(layer_list), cur_(cur) {}
    DirtyLayerIterator(const DirtyLayerIterator&) = default;
    DirtyLayerIterator(DirtyLayerIterator&&) = default;
    DirtyLayerIterator& operator=(const DirtyLayerIterator&) = default;
    DirtyLayerIterator& operator=(DirtyLayerIterator&&) = default;
    LayerImpl* operator*() { return layer_list_->find(*cur_)->get(); }
    DirtyLayerIterator& operator++() {
      ++cur_;
      return *this;
    }
    DirtyLayerIterator operator++(int) {
      DirtyLayerIterator result(*this);
      ++*this;
      return result;
    }

   private:
    raw_ref<const OwnedLayerImplList> layer_list_;
    SetType::const_iterator cur_;
  };
  class DirtyLayerRange {
   public:
    explicit DirtyLayerRange(const OwnedLayerImplList& layer_list)
        : layer_list_(layer_list) {}
    DirtyLayerRange(const DirtyLayerRange&) = default;
    DirtyLayerRange(DirtyLayerRange&&) = default;
    DirtyLayerRange& operator=(const DirtyLayerRange&) = default;
    DirtyLayerRange& operator=(DirtyLayerRange&&) = default;
    DirtyLayerIterator begin() const {
      return {*layer_list_,
              layer_list_->layers_that_should_push_properties_.begin()};
    }
    DirtyLayerIterator end() const {
      return {*layer_list_,
              layer_list_->layers_that_should_push_properties_.end()};
    }
    size_type size() const {
      return layer_list_->layers_that_should_push_properties_.size();
    }

   private:
    raw_ref<const OwnedLayerImplList> layer_list_;
  };
  static_assert(std::ranges::range<DirtyLayerRange>);
```


The `static_assert` fails with:

../../cc/layers/layer_collections.h:164:17: error: static assertion failed
  164 |   static_assert(std::ranges::range<DirtyLayerRange>);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../cc/layers/layer_collections.h:164:17: note: because 'DirtyLayerRange' does not satisfy 'range'
gen/third_party/libc++/src/include/__ranges/concepts.h:52:3: note: because 'ranges::begin(__t)' would be invalid: call to deleted function call operator in type 'const __begin::__fn'
   52 |   ranges::begin(__t); // sometimes equality-preserving
      |   ^
1 error generated.


SOS!

Daniel Cheng

unread,
Mar 17, 2026, 2:19:25 PMMar 17
to Stefan Zager, Victor Vianna, cxx
1. Your iterator probably needs to be default constructible, though I think that's usually a problem for `std::ranges::end()`, not `std::ranges::begin()`.
2. If you write out the `std::ranges::begin()` call explicitly, clang will usually tell you why `std::ranges::begin()` would be invalid in a more helpful way. Sadly the actual error from `std::ranges::ranges<SomeRangeTYpe>` is not that helpful most of the time.

(If you have a draft CL uploaded somewhere, I can help as well)

Daniel

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHOQ7J8KjJJQ0BoWU_rNp1EKH5E3_PE%2BRTzvgA4%2BZi%3DAOVdYWg%40mail.gmail.com.

Daniel Cheng

unread,
Mar 17, 2026, 6:22:55 PMMar 17
to Stefan Zager, Victor Vianna, cxx
I've also filed crbug.com/493534962 to consider allowing more use of range views.

Daniel
Reply all
Reply to author
Forward
0 new messages