Увеличить преобразование const_iterator в итератор - PullRequest
0 голосов
/ 09 мая 2018

Я попытался двумя способами реализовать преобразование из const_iterator в итератор. Все итераторы основаны на boost/iterator.

Метод 1 определяет класс iterator<T>. iterator<const T> будет представлять const_iterator. iterator<T> имеет оператор преобразования, который возвращает iterator<const T>. Это не работает для функции шаблона, потому что во время создания шаблона преобразование не может быть выполнено

Метод 2 работает в теории. На практике мне нужно определить каждый метод для iterator<T>:

#include <iostream>
#include <boost/iterator/iterator_adaptor.hpp>
#include <vector>

template<typename Container>
class Cit
        : public boost::iterator_adaptor<
                Cit<Container>, // Derived
                typename Container::const_iterator, // Base
                const typename Container::value_type> {
    using self_type = Cit<Container>;
    friend class boost::iterator_core_access;
public:
    explicit Cit(typename Container::const_iterator it)
            : self_type::iterator_adaptor_(it) {}
};


template<typename Container>
class It : public Cit<Container> {
protected:
    using reference = typename Container::reference;
    using self_type = It<Container>;
    using Base = Cit<Container>;
public:
    explicit It(typename Container::iterator it)
            : Base(it) {}

    reference operator*() const {
        return const_cast<reference>(Base::operator*());
    }
    // Try to hide every method from Cit<Container>
    // ... 
    // ... 
    // ... 
    // ... oh well.
private:
    friend class boost::iterator_core_access;
};

// A template function
template<typename Container>
void foo(Cit<Container> it_begin,
         Cit<Container> it_end) {
    for (auto it = it_begin; it != it_end; ++it) {
        std::cout << *it << "\n";
    }

}

int main() {
    typedef std::vector<int> Container;
    Container v = {0, 1, 2, 3};  // content array
    It<Container> it_begin(v.begin());
    It<Container> it_end(v.end());
    // Assert It can implicitly convert to Cit even during template 
    // instantiation.
    foo(it_begin, it_end);
    return 0;
}

Кажется, это сводит на нет преимущества использования boost/iterator.

Есть ли лучший способ сделать iterator и const_iterator с boost/iterator

Вот метод 1:

#include <iostream>
#include <boost/iterator/iterator_adaptor.hpp>
#include <vector>

template<typename Container>
class It
        : public boost::iterator_adaptor<
                It<Container>, // Derived
                typename Container::const_iterator, // Base
                typename std::conditional<std::is_const<Container>::value,
                        const typename Container::value_type,
                        typename Container::value_type
                        >::type // Value
        > {
    using self_type = It<Container>;
    friend class boost::iterator_core_access;
public:
    explicit It(typename Container::const_iterator it)
            : self_type::iterator_adaptor_(it) {}
};

template <typename C> using Cit = It<const C>;

// A template function
template<typename Container>
void foo(Cit<Container> it_begin,
         Cit<Container> it_end) {
    for (auto it = it_begin; it != it_end; ++it) {
        std::cout << *it << "\n";
    }

}

int main() {
    typedef std::vector<int> Container;
    Container v = {0, 1, 2, 3};  // content array
    It<Container> it_begin(v.begin());
    It<Container> it_end(v.end());
    // Assert It can implicitly convert to from Cit to It even
    // during template instantiation.
    foo(it_begin, it_end);
    return 0;
}

Сообщение об ошибке:

error: no matching function for call to ‘foo(It<std::vector<int> >&, It<std::vector<int> >&)’
     foo(it_begin, it_end);
                         ^
main.cpp:26:6: note: candidate: template<class Container> void foo(Cit<Container>, Cit<Container>)
 void foo(Cit<Container> it_begin,
      ^~~
main.cpp:26:6: note:   template argument deduction/substitution failed:
main.cpp:41:25: note:   types ‘const C’ and ‘std::vector<int>’ have incompatible cv-qualifiers
     foo(it_begin, it_end);

1 Ответ

0 голосов
/ 09 мая 2018

Я бы специализировал шаблон:

template <typename T>
class MyIt : public boost::iterator_adaptor<MyIt<T>,              // Derived
                                            typename T::iterator, // Base
                                            typename T::reference> {
    friend class boost::iterator_core_access;

  public:
    static constexpr bool is_const = false;
    explicit MyIt(typename MyIt::base_type it) : MyIt::iterator_adaptor_(it) {}
};

template <typename T>
class MyIt<T const> : public boost::iterator_adaptor<MyIt<T const>,              // Derived
                                                     typename T::const_iterator, // Base
                                                     typename T::const_reference> {
    friend class boost::iterator_core_access;

  public:
    static constexpr bool is_const = true;

    explicit MyIt(typename MyIt::base_type it) : MyIt::iterator_adaptor_(it) {}
};

Преобразования здесь уже разрешены, но если вы хотите иметь явное "to-const-cast", то легко написать:

template <typename T>
static MyIt<T const> make_const(MyIt<T> it) { return MyIt<T const>(it.base()); }

Используйте это:

// A template function
template <typename It> void foo(It it_begin, It it_end) {
    static_assert(It::is_const == std::is_const<typename std::remove_reference<decltype(*it_begin)>::type>::value, "mismatch");
    if (It::is_const)
        std::cout << "Const: ";

    for (auto it = it_begin; it != it_end; ++it)
        std::cout << *it << " ";

    std::cout << "\n";
}

Как видите, наша функция не заботится о конкретном итераторе (в этом весь смысл итераторов). Вы можете использовать его с const и non-const:

template <typename C> void foo(C const &c) {
    MyIt<C const> b(c.begin()), e(c.end());
    foo(b, e);
}

template <typename C> void foo(C &c) {
    MyIt<C> b(c.begin()), e(c.end());
    foo(b, e);
}

Быстрое демо Live On Coliru

 std::vector<int> v{ 0, 1, 2, 3 };
 foo(v);

 auto const &constv = v;
 foo(constv);

печать

void foo(C&) [with C = std::vector<int>]
0 1 2 3 
void foo(const C&) [with C = std::vector<int>]
Const: 0 1 2 3

Форсирование константных итераторов

Похоже, это ваш код. Итак, давайте заставим это! Это простое изменение MyIt<C> на MyIt<C const>:

template <typename C> void foo(C &c) {
    MyIt<C const> b(c.begin()), e(c.end()); //  <--- note the const
    foo(b, e);
}

Теперь foo будет вызываться с использованием константных итераторов даже для неконстантных C. Если вы считаете, что это не так, вы можете использовать помощник, показанный выше:

template <typename C> void foo(C &c) {
    MyIt<C> b(c.begin()), e(c.end()); 
    foo(make_const(b), make_const(e)); //  <--- now more explicit?
}

Конечно, в foo вы можете модифицировать static_assert, чтобы он просто отказывался компилировать для неконстантных итераторов, во-первых:

// A template function
template <typename It> void foo(It it_begin, It it_end) {
    static_assert(std::is_const<typename std::remove_reference<decltype(*it_begin)>::type>::value, "non-const disallowed");
    if (It::is_const)
        std::cout << "Const: ";

    for (auto it = it_begin; it != it_end; ++it)
        std::cout << *it << " ";

    std::cout << "\n";
}

Вы можете добавить перегрузку для любого MyIt<>, который выполняет консолидацию:

template <typename C> 
typename std::enable_if<!std::is_const<C>::value>::type
foo(MyIt<C> b, MyIt<C> e) {
    foo(make_const(b), make_const(e));
}

Итак, теперь каждый вызов foo переводится в режим const.

Полный список

Последняя полная демонстрация:

Live On Coliru

#include <boost/iterator/iterator_adaptor.hpp>
#include <iostream>
#include <vector>

template <typename T>
class MyIt : public boost::iterator_adaptor<MyIt<T>,              // Derived
                                            typename T::iterator, // Base
                                            typename T::reference> {
    friend class boost::iterator_core_access;

  public:
    static constexpr bool is_const = false;
    explicit MyIt(typename MyIt::base_type it) : MyIt::iterator_adaptor_(it) {}
};

template <typename T>
class MyIt<T const> : public boost::iterator_adaptor<MyIt<T const>,              // Derived
                                                     typename T::const_iterator, // Base
                                                     typename T::const_reference> {
    friend class boost::iterator_core_access;

  public:
    static constexpr bool is_const = true;

    explicit MyIt(typename MyIt::base_type it) : MyIt::iterator_adaptor_(it) {}
};

template <typename T>
static MyIt<T const> make_const(MyIt<T> it) { return MyIt<T const>(it.base()); }

// A template function
template <typename It> void foo(It it_begin, It it_end) {
    static_assert(std::is_const<typename std::remove_reference<decltype(*it_begin)>::type>::value, "non-const disallowed");
    if (It::is_const)
        std::cout << "Const: ";

    for (auto it = it_begin; it != it_end; ++it)
        std::cout << *it << " ";

    std::cout << "\n";
}

template <typename C> 
typename std::enable_if<!std::is_const<C>::value>::type
foo(MyIt<C> b, MyIt<C> e) {
    foo(make_const(b), make_const(e));
}

template <typename C> void foo(C &c) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
    MyIt<C> b(c.begin()), e(c.end());
    foo(b, e);
}

int main() {
    std::vector<int> v{ 0, 1, 2, 3 };
    foo(v);

    auto const &constv = v;
    foo(constv);
}

Который сейчас печатает:

void foo(C&) [with C = std::vector<int>]
Const: 0 1 2 3 
void foo(C&) [with C = const std::vector<int>]
Const: 0 1 2 3 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...