Sean Silva
2014-Mar-04 22:51 UTC
[LLVMdev] [cfe-dev] C++11 reverse iterators (was C++11 is here)
On Mon, Mar 3, 2014 at 12:26 AM, Chris Lattner <sabre at nondot.org> wrote:> > On Mar 2, 2014, at 8:53 PM, Renato Golin <renato.golin at linaro.org> wrote: > > > On 3 March 2014 12:32, Pete Cooper <peter_cooper at apple.com> wrote: > >> Would those work with a foreach construct? Perhaps I forgot to mention > that was what I'm trying to work out here. > >> > >> In example 3 I was wondering if we could define a method reverse(). We > could use sfinae to wrap that around rbegin/rend if people like that style? > > > > Sorry, I was too terse... ;) > > > > If MF is a reverse_iterator, it'd just work, no? But to get the > > reverse iterator, I think reverse() would be the best general pattern, > > since you can adapt it to each container needs. > > I'm not aware of the prior art or standards are here, but I think that a > global reverse() adapter is the way to go. Likewise, we should have a > standard "enumerate()" adaptor like python. >`enumerate` is going to be so much more annoying to use without tuple unpacking in the `for` syntax :( -- Sean Silva> > -Chris > _______________________________________________ > cfe-dev mailing list > cfe-dev at cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140304/426aa83f/attachment.html>
Chris Lattner
2014-Mar-04 23:11 UTC
[LLVMdev] [cfe-dev] C++11 reverse iterators (was C++11 is here)
On Mar 4, 2014, at 2:51 PM, Sean Silva <silvas at purdue.edu> wrote:> > > > On Mon, Mar 3, 2014 at 12:26 AM, Chris Lattner <sabre at nondot.org> wrote: > > On Mar 2, 2014, at 8:53 PM, Renato Golin <renato.golin at linaro.org> wrote: > > > On 3 March 2014 12:32, Pete Cooper <peter_cooper at apple.com> wrote: > >> Would those work with a foreach construct? Perhaps I forgot to mention that was what I'm trying to work out here. > >> > >> In example 3 I was wondering if we could define a method reverse(). We could use sfinae to wrap that around rbegin/rend if people like that style? > > > > Sorry, I was too terse... ;) > > > > If MF is a reverse_iterator, it'd just work, no? But to get the > > reverse iterator, I think reverse() would be the best general pattern, > > since you can adapt it to each container needs. > > I'm not aware of the prior art or standards are here, but I think that a global reverse() adapter is the way to go. Likewise, we should have a standard "enumerate()" adaptor like python. > > `enumerate` is going to be so much more annoying to use without tuple unpacking in the `for` syntax :(Sure, it won't be as nice as Python, but in my limited experience with C++'11 foreach loops, the #1 reason I can't use them is because there is no way to parallel iterate two vectors (as a random example) which would be simple if an index were available. It doesn't matter to me how syntactically convenient it is to get the index, so long as it is possible. -Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140304/28a62c2c/attachment.html>
Sean Silva
2014-Mar-04 23:38 UTC
[LLVMdev] [cfe-dev] C++11 reverse iterators (was C++11 is here)
On Tue, Mar 4, 2014 at 6:11 PM, Chris Lattner <sabre at nondot.org> wrote:> > On Mar 4, 2014, at 2:51 PM, Sean Silva <silvas at purdue.edu> wrote: > > > > > On Mon, Mar 3, 2014 at 12:26 AM, Chris Lattner <sabre at nondot.org> wrote: > >> >> On Mar 2, 2014, at 8:53 PM, Renato Golin <renato.golin at linaro.org> wrote: >> >> > On 3 March 2014 12:32, Pete Cooper <peter_cooper at apple.com> wrote: >> >> Would those work with a foreach construct? Perhaps I forgot to mention >> that was what I'm trying to work out here. >> >> >> >> In example 3 I was wondering if we could define a method reverse(). We >> could use sfinae to wrap that around rbegin/rend if people like that style? >> > >> > Sorry, I was too terse... ;) >> > >> > If MF is a reverse_iterator, it'd just work, no? But to get the >> > reverse iterator, I think reverse() would be the best general pattern, >> > since you can adapt it to each container needs. >> >> I'm not aware of the prior art or standards are here, but I think that a >> global reverse() adapter is the way to go. Likewise, we should have a >> standard "enumerate()" adaptor like python. >> > > `enumerate` is going to be so much more annoying to use without tuple > unpacking in the `for` syntax :( > > > Sure, it won't be as nice as Python, but in my limited experience with > C++'11 foreach loops, the #1 reason I can't use them is because there is no > way to parallel iterate two vectors (as a random example) which would be > simple if an index were available. It doesn't matter to me how > syntactically convenient it is to get the index, so long as it is possible. >Sure. It definitely won't be unbearable. Iterating two vectors in parallel also suggests a variadic `auto zip(...) -> std::tuple<...>` helper. -- Sean Silva> > -Chris > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140304/ab21d909/attachment.html>
Howard Hinnant
2014-Mar-04 23:43 UTC
[LLVMdev] [cfe-dev] C++11 reverse iterators (was C++11 is here)
On Mar 4, 2014, at 6:11 PM, Chris Lattner <sabre at nondot.org> wrote:> Sure, it won't be as nice as Python, but in my limited experience with C++'11 foreach loops, the #1 reason I can't use them is because there is no way to parallel iterate two vectors (as a random example) which would be simple if an index were available. It doesn't matter to me how syntactically convenient it is to get the index, so long as it is possible.Thrown together… #include <cstddef> class index_range { std::size_t begin_; std::size_t end_; public: class const_iterator { std::size_t i_; public: explicit constexpr const_iterator(std::size_t i) noexcept : i_(i) {} constexpr std::size_t operator*() const noexcept {return i_;} const_iterator& operator++() noexcept {++i_; return *this;} friend constexpr bool operator==(const_iterator x, const_iterator y) noexcept {return x.i_ == y.i_;} friend constexpr bool operator!=(const_iterator x, const_iterator y) noexcept {return !(x == y);} }; constexpr index_range(std::size_t begin, std::size_t end) noexcept : begin_(begin) , end_(end) {} constexpr const_iterator begin() const noexcept {return const_iterator(begin_);} constexpr const_iterator end() const noexcept {return const_iterator(end_);} }; #include <vector> #include <cassert> int main() { std::vector<int> v1(3); std::vector<int> v2 = v1; assert(v1.size() == v2.size()); for (auto i : index_range(0, v1.size())) { v1[i] = i; v2[i] = i; } assert(v1 == std::vector<int>({0, 1, 2})); assert(v2 == std::vector<int>({0, 1, 2})); } Admittedly, you don’t want to write index_range every time you want to form such a loop. But if it were a easily available utility… Howard