设置
当你想让迭代器在返回之前处理它们正在迭代的内容时,boost::transform_iterator
都不错。您向它们传递一个一元函数,该函数转换底层迭代器的 operator*()
的结果。然后转换迭代器返回:
template<typename Map>
struct iterator_transform_traits_map_second {
typedef typename Map::value_type value_type;
typedef typename Map::mapped_type result_type;
result_type& operator()( value_type& v) const {return v.second;}
const result_type& operator()(const value_type& v) const {return v.second;}
};
typedef
boost::transform_iterator<iterator_transform_traits_map_second>
transformed_iterator;
到目前为止,还不错。但是。
这会导致什么困惑
您的同事喜欢这个 Shiny 的新工具,并且也开始使用它,很快就会有人在标题中收集您到目前为止所提出的所有内容。这是我们的:
-
iterator_transform_traits_map_first
-
iterator_transform_traits_map_second
-
iterator_transform_traits_map_deref
(取消引用任何容器的条目) -
iterator_transform_traits_map_deref_second
(取消引用 map 条目的second
) -
iterator_transform_traits_map_dynamic_cast
(执行dynamic_cast<>()
任何容器的条目) -
iterator_transform_traits_map_any_second
(对 map 条目的any_cast<>()
执行second
)
当然,这遗漏了很多有用的东西(因为还没有人需要它们),而且根本无法扩展。我只是负责编写一个迭代器来取消引用 map 条目的 second
并执行 dynamic_cast<>()
, 而我是我拒绝添加 iterator_transform_traits_map_dynamic_cast_deref_second
的人继续前进。
我想要什么
相反,我尝试编写一些基本特征和一个编译时组合特征,允许将其中几个命名为模板参数并简单地通过管道调用.理想情况下,我想要这样的东西:
typedef
boost::transform_iterator<
iterator_transform_traits< iter_transf_tr_second
, iter_transf_tr_deref
, iter_transf_tr_dynamic_cast<derived>
>
>
transformed_iterator;
我目前的想法是递归地派生一个包装器模板,然后递归地调用所有特征,将输出从一个传递到下一个。十年前我做过这样的事情,并且对如何做有一个基本的想法。然而,我最后一次这样做是步行。也就是说,我自己实现了所有模板元魔术。
当然,这很愚蠢,因为我们现在有 boost.mpl、boost.fusion 等,所以我宁愿使用已经存在的东西。但是,经过一个下午的修改后,很明显在完成此操作之前我需要学习很多东西。虽然我不反对学习所有这些,但我的脖子上有人喜欢我所做的事情,但他说无论如何他都需要拔掉插头,因为有这个截止日期......我现在可以选择只是写他妈的iterator_transform_traits_map_dynamic_cast_deref_second
,复制大量已经腐烂了十年并以此为基础构建的代码,或者提出一个干净的解决方案。
这就是你进来的地方。
问题
您将如何利用已有的特性来实现这个复合特性?
平台
但是,有一个问题:我们在一个嵌入式平台上并且坚持使用 GCC 4.1.2,这意味着 C++ 03、TR1 和 boost 1.52。没有变量模板参数,没有 decltype
以及所有那些花哨的东西。
最佳答案
给你:
iterator_transform_traits.hpp
#include <boost/mpl/vector.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/container/vector/convert.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/ref.hpp>
template<typename IteratorTraitsSequence, typename Container>
class iterator_transform_traits
{
public:
struct type
{
private:
struct plcaholder_resolver
{
template<typename IteratorTraits, typename IteratorLambda>
struct apply
{
typedef typename boost::mpl::push_back<IteratorTraits,
typename boost::mpl::apply<typename boost::mpl::lambda<IteratorLambda>::type, typename boost::mpl::back<IteratorTraits>::type::result_type>::type>::type type;
};
};
struct begin_value
{
typedef typename Container::value_type result_type;
};
typedef typename boost::mpl::pop_front<typename boost::mpl::fold<IteratorTraitsSequence, boost::mpl::vector<begin_value>, plcaholder_resolver>::type>::type iterator_traits;
public:
typedef typename boost::mpl::front<iterator_traits>::type::value_type value_type;
typedef typename boost::mpl::back<iterator_traits>::type::result_type result_type;
public:
struct recursive_iterator_modifier
{
template<class> struct result;
template<class F, typename CurrentResult, typename IteratorTrait>
struct result<F(CurrentResult&, const IteratorTrait&)>
{
typedef typename IteratorTrait::result_type& type;
};
template<class F, typename CurrentResult, typename IteratorTrait>
struct result<F(const CurrentResult&, const IteratorTrait&)>
{
typedef const typename IteratorTrait::result_type& type;
};
template<class F, typename CurrentResult, typename IteratorTrait>
struct result<F(const boost::reference_wrapper<CurrentResult>&, const IteratorTrait&)>
{
typedef typename IteratorTrait::result_type& type;
};
template<typename CurrentResult, typename IteratorTrait>
typename IteratorTrait::result_type&
operator()(CurrentResult& modified, const IteratorTrait& it)
{
return (it(modified));
}
template<typename CurrentResult, typename IteratorTrait>
const typename IteratorTrait::result_type&
operator()(const CurrentResult& modified, const IteratorTrait& it)
{
return (it(modified));
}
template<typename CurrentResult, typename IteratorTrait>
typename IteratorTrait::result_type&
operator()(const boost::reference_wrapper<CurrentResult>& modified, const IteratorTrait& it)
{
return (it(modified.get()));
}
};
public:
result_type& operator()(value_type& v) const
{
return boost::fusion::fold(iterator_traits_vector_, boost::ref(v), recursive_iterator_modifier());
}
const result_type& operator()(const value_type& v) const
{
return boost::fusion::fold(iterator_traits_vector_, boost::ref(v), recursive_iterator_modifier());
}
private:
typedef typename boost::fusion::result_of::as_vector<iterator_traits>::type iterator_traits_vector;
iterator_traits_vector iterator_traits_vector_;
};
};
你可以这样使用它:
#include <map>
#include <string>
#include <iostream>
#include <typeinfo>
#include "iterator_transform_traits.hpp"
template<typename Pair>
struct iterator_transform_traits_map_second {
typedef Pair value_type;
typedef typename Pair::second_type result_type;
result_type& operator()( value_type& v) const {return v.second;}
const result_type& operator()(const value_type& v) const {return v.second;}
};
template<typename Dereferenced>
struct iterator_transform_traits_deref {};
template<typename Dereferenced>
struct iterator_transform_traits_deref<Dereferenced*> {
typedef Dereferenced* value_type;
typedef Dereferenced result_type;
result_type& operator()( value_type& v) const {return *v;}
const result_type& operator()(const value_type& v) const {return *v;}
};
typedef std::map<std::string, std::string*> string_ptr_map;
typedef iterator_transform_traits<boost::mpl::vector<iterator_transform_traits_map_second<boost::mpl::_1>, iterator_transform_traits_deref<boost::mpl::_1> >, string_ptr_map>::type Transformer;
typedef boost::transform_iterator<Transformer, string_ptr_map::iterator> string_ptr_map_second_deref_iterator;
int main()
{
string_ptr_map map;
map["key1"] = new std::string("value1");
map["key2"] = new std::string("value2");
map["key3"] = new std::string("value3");
for(string_ptr_map_second_deref_iterator it(map.begin(), Transformer()), ite(map.end(), Transformer()); it != ite; ++it)
{
std::cout << *it << std::endl;
}
return 0;
}
现在一些信息:
- 每个
iterator_transform_trait
都必须在他将作为参数接收的value_type
上进行模板化,而不是在容器上,否则您无法自动链接它们。 - 有很多使用
boost::mpl
和boost::fusion
的小元编程操作,我希望元函数名称足够明确,请随时提问问题 - 您需要使用
mpl
序列作为模板参数,因为您无权访问 C++11 和可变参数模板。 - 这是一个在线编译版本:http://rextester.com/ZIYG56999
- 这是一个有趣的练习,让我有些头疼:)
关于c++ - 为 boost 的变换迭代器寻找复合特征模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21452523/