Namespaces
Variants
Views
Actions

Difference between revisions of "Talk:cpp/iterator/iterator"

From cppreference.com
(re)
m
 
(One intermediate revision by one user not shown)
Line 3: Line 3:
 
[[Special:Contributions/194.74.130.171|194.74.130.171]] 00:25, 20 May 2021 (PDT)
 
[[Special:Contributions/194.74.130.171|194.74.130.171]] 00:25, 20 May 2021 (PDT)
 
: {{lc|std::iterator}} is just a class template that declares the template type arguments you give it as type aliases. Basically it's completely useless, as it serves to simply to obscure the purpose of those types when they could simply be provided explicitly by the custom iterator class instead (which is the alternative). Furthermore, from an object oriented perspective, I think it's non-sensical to use inheritance here; I don't think there's any use case for wanting to take a e.g. {{lc|std::iterator<std::input_iterator_tag, long, long, const long*, long>}} as say a function parameter. --[[User:Ybab321|Ybab321]] ([[User talk:Ybab321|talk]]) 04:48, 20 May 2021 (PDT)
 
: {{lc|std::iterator}} is just a class template that declares the template type arguments you give it as type aliases. Basically it's completely useless, as it serves to simply to obscure the purpose of those types when they could simply be provided explicitly by the custom iterator class instead (which is the alternative). Furthermore, from an object oriented perspective, I think it's non-sensical to use inheritance here; I don't think there's any use case for wanting to take a e.g. {{lc|std::iterator<std::input_iterator_tag, long, long, const long*, long>}} as say a function parameter. --[[User:Ybab321|Ybab321]] ([[User talk:Ybab321|talk]]) 04:48, 20 May 2021 (PDT)
 +
:: Here's the example from the page without {{lc|std::iterator}}
 +
{{source|1=
 +
#include <iostream>
 +
#include <iterator>
 +
 +
template<long FROM, long TO>
 +
class Range {
 +
public:
 +
    // member typedefs provided clearly and explicitly
 +
    class iterator {
 +
    public:
 +
        using iterator_category = std::input_iterator_tag;
 +
        using value_type = long;
 +
        using difference_type = long;
 +
        using pointer = const long*;
 +
        using reference = long;
 +
       
 +
    private:
 +
        long num = FROM;
 +
    public:
 +
        explicit iterator(long _num = 0) : num(_num) {}
 +
        iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
 +
        iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}
 +
        bool operator==(iterator other) const {return num == other.num;}
 +
        bool operator!=(iterator other) const {return !(*this == other);}
 +
        reference operator*() const {return num;}
 +
    };
 +
    iterator begin() {return iterator(FROM);}
 +
    iterator end() {return iterator(TO >= FROM? TO+1 : TO-1);}
 +
};
 +
}}
 +
:: --[[User:Ybab321|Ybab321]] ([[User talk:Ybab321|talk]]) 04:58, 20 May 2021 (PDT)

Latest revision as of 03:58, 20 May 2021

Why is it deprecated? What's the recommended alternative? 194.74.130.171 00:25, 20 May 2021 (PDT)

std::iterator is just a class template that declares the template type arguments you give it as type aliases. Basically it's completely useless, as it serves to simply to obscure the purpose of those types when they could simply be provided explicitly by the custom iterator class instead (which is the alternative). Furthermore, from an object oriented perspective, I think it's non-sensical to use inheritance here; I don't think there's any use case for wanting to take a e.g. std::iterator<std::input_iterator_tag, long, long, const long*, long> as say a function parameter. --Ybab321 (talk) 04:48, 20 May 2021 (PDT)
Here's the example from the page without std::iterator
#include <iostream>
#include <iterator>
 
template<long FROM, long TO>
class Range {
public:
    // member typedefs provided clearly and explicitly
    class iterator {
    public:
        using iterator_category = std::input_iterator_tag;
        using value_type = long;
        using difference_type = long;
        using pointer = const long*;
        using reference = long;
 
    private:
        long num = FROM;
    public:
        explicit iterator(long _num = 0) : num(_num) {}
        iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
        iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}
        bool operator==(iterator other) const {return num == other.num;}
        bool operator!=(iterator other) const {return !(*this == other);}
        reference operator*() const {return num;}
    };
    iterator begin() {return iterator(FROM);}
    iterator end() {return iterator(TO >= FROM? TO+1 : TO-1);}
};
--Ybab321 (talk) 04:58, 20 May 2021 (PDT)