我的继承自boost::iterator_adaptor
的类的代码类似于
private:
using is_bidirectional = std::is_convertible<
iterator_category, std::bidirectional_iterator_tag>;
typename std::enable_if<is_bidirectional::value>::type
decrement() { ... }
我想知道enable_if
部分是否多余。如上所述,如果适配的迭代器不是双向的,则不会启用decrement()
重载。但是我不知道boost::iterator_adaptor
是否已经使用SFINAE,因此我不需要关注私有重载中的遍历类别。代码编译并正常工作。我只是询问enable_if par
t是否有用或是否多余。
答案 0 :(得分:2)
是
实际上,默认情况下,适配器会镜像base-iterator的类别。
这是一项有效的综合测试:
<强> Live On Coliru 强>
#include <boost/iterator_adaptors.hpp>
template <typename BaseIterator>
struct Adapt : boost::iterator_adaptor<Adapt<BaseIterator>, BaseIterator> {
using base = boost::iterator_adaptor<Adapt<BaseIterator>, BaseIterator>;
using base::base;
using base::operator=;
};
#include <iostream>
#include <iterator>
#include <typeinfo>
template <typename It>
std::string cat(It const&) {
return typeid(typename std::iterator_traits<It>::iterator_category).name();
}
template <typename It, typename OutIt>
void test(std::string caption, It f, It l, OutIt out) {
Adapt<It> af{f}, al{l};
std::cout << caption << ", adapts to " << cat(af) << "\t";
std::copy(af, al, out);
std::cout << "\n";
}
template <typename Container, typename OutIt>
void test(std::string caption, Container const& c, OutIt out) {
using BaseIterator = typename Container::const_iterator;
Adapt<BaseIterator> f{c.begin()}, l{c.end()};
test(caption, f, l, out);
}
#include <vector>
#include <forward_list>
#include <list>
#include <sstream>
int main() {
using V = std::vector<int>;
using L = std::list<int>;
using FL = std::forward_list<int>;
using II = std::istream_iterator<int>;
using OI = std::ostream_iterator<int>;
static_assert(std::is_same<std::random_access_iterator_tag, std::iterator_traits<V::iterator>::iterator_category>{}, "");
static_assert(std::is_same<std::bidirectional_iterator_tag, std::iterator_traits<L::iterator>::iterator_category>{}, "");
static_assert(std::is_same<std::forward_iterator_tag, std::iterator_traits<FL::iterator>::iterator_category>{}, "");
static_assert(std::is_same<std::input_iterator_tag, std::iterator_traits<II>::iterator_category>{}, "");
static_assert(std::is_same<std::output_iterator_tag, std::iterator_traits<OI>::iterator_category>{}, "");
OI out(std::cout, " ");
test("vector ", V { 1,2,3 }, out);
test("list ", L { 4,5,6 }, out);
test("forward_list", FL { 7,8,9 }, out);
{
std::istringstream iss("10 11 12");
II f(iss), l;
test("stream input", f, l, out);
}
}
打印¹
vector , adapts to std::random_access_iterator_tag 1 2 3
list , adapts to std::bidirectional_iterator_tag 4 5 6
forward_list, adapts to std::forward_iterator_tag 7 8 9
stream input, adapts to std::input_iterator_tag 10 11 12
¹通过c++filt -t
过滤了输出,以获得更漂亮的类型信息