Reputation: 2064
I tried two methods to implement conversion from a const_iterator to an iterator. All iterators are based on boost/iterator
.
Method 1 defines a iterator<T>
class. iterator<const T>
would represent a const_iterator
. iterator<T>
has a conversion operator that returns a iterator<const T>
. This fails for template function because no type conversion can happen during template instantiation.
Method 2 works in theory. In practice, I need to define every method for the 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;
}
This seems to negate the benefits of using boost/iterator
.
Is there a better way to make
iterator
andconst_iterator
withboost/iterator
?
Here is method 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 message:
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);
Upvotes: 3
Views: 710
Reputation: 392883
I would specialize the template:
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) {}
};
Conversions are already allowed here, but if you want to have an explicit "to-const-cast" it's easy to write:
template <typename T>
static MyIt<T const> make_const(MyIt<T> it) { return MyIt<T const>(it.base()); }
Use it:
// 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";
}
As you can see our function doesn't care about the specific iterator (this is the whole point of iterators). You can use it with const and 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);
}
Quick Demo Live On Coliru
std::vector<int> v{ 0, 1, 2, 3 }; foo(v); auto const &constv = v; foo(constv);
Prints
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
This seems to be what your code is about. So, let's force that! It's a simple change of MyIt<C>
to 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);
}
Now foo will be called using const iterators even for non-const C. If you think that's subtle, you can use the helper shown above:
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?
}
Of course in foo
you're free to modify the static_assert
so that it just refuses to compile for non-const iterators in the first place:
// 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";
}
You can add an overload for any MyIt<>
that does the constification:
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));
}
So, now every invocation of foo
is forced to const
mode.
The last demo in full:
#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);
}
Which now prints:
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
Upvotes: 1