sbi*_*sbi 10 c++ boost traits c++03
如果你想让迭代器在返回之前与它们迭代的东西一起旋转,那就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;
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但.
你的同事喜欢这个闪亮的新工具,并且也开始使用它,很快就会有人收集你们目前为止所提出的内容.这是我们的:
iterator_transform_traits_map_firstiterator_transform_traits_map_seconditerator_transform_traits_map_deref (取消引用任何容器的条目)iterator_transform_traits_map_deref_second(取消引用地图条目second)iterator_transform_traits_map_dynamic_cast(执行dynamic_cast<>()任何容器的输入)iterator_transform_traits_map_any_second(执行any_cast<>()地图条目second)当然,这留下了许多有用的(因为还没有人需要它们),并且它根本不能扩展.我的任务是编写一个迭代器来取消引用地图条目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;
Run Code Online (Sandbox Code Playgroud)
我目前的想法是递归地派生一个包装器模板,并以递归方式调用所有特征,将输出从一个传递给下一个.十年前我做过类似的事情,对如何做到这一点有一个基本的想法.但是,我最后一次这样做是徒步.也就是说,我自己实现了所有模板元魔法.
当然,这很愚蠢,因为我们现在有boost.mpl,boost.fusion等,所以我宁愿使用已有的东西.然而,经过一个下午的修补之后,很明显我在完成这项工作之前还有很多东西需要学习.虽然我不反对学习所有这些,但我有一个人喜欢我的工作,但他说他还是需要拔掉插头,因为这个截止日期......我现在可以选择写下该死的iterator_transform_traits_map_dynamic_cast_deref_second,复制大量已腐烂十年的代码,并在此基础上构建,或者提出一个干净的解决方案.
这就是你进来的地方.
您将如何利用已有的复合特征来实现这些复合特征?
然而,有一个问题:我们在嵌入式平台上并坚持使用GCC 4.1.2,这意味着C++ 03,TR1和boost 1.52.没有变量模板参数,没有decltype和所有那些花哨的东西.
干得好:
#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_;
};
};
Run Code Online (Sandbox Code Playgroud)
你像这样使用它:
#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;
}
Run Code Online (Sandbox Code Playgroud)
现在一些信息:
iterator_transform_trait必须在value_type他将作为参数接收的参数上进行模板化,而不是在容器上进行模板化,否则你无法自动链接它们。boost::mpland的小元编程操作boost::fusion,我希望元函数名称足够明确,请随意提出任何问题mpl序列作为模板参数,因为您无权访问 C++11 和可变参数模板。