增强元组部分迭代
boost tuple partial iteration?
我试图使用boost元组来避免一些虚拟函数开销,但我不能完全使其工作。我有一个尝试处理输入的"处理程序"向量,一旦其中一个返回true,我就不想调用其余的。
c++ 11不可用
首先,当前的虚拟实现是这样的:std::vector<Handler*> handlers;
//initialization code...
handlers.push_back(new Handler1);
handlers.push_back(new Handler2);
handlers.push_back(new Handler3);
//runtime code
for(std::vector<Handler*>::iterator iter = handlers.begin();
iter != handlers.end() && !(*iter)->handle(x); ++iter) {}
由于我在编译时拥有所有类型,所以我希望能够将其表示为元组,如:
boost::tuple<Handler1,Handler2,Handler3> handlers;
//runtime code
???
// should compile to something equivalent to:
// if(!handlers.get<0>().handle(x))
// if(!handlers.get<1>().handle(x))
// handlers.get<2>().handle(x);
理想情况下,没有虚函数调用,任何空函数体都将内联出。似乎这几乎可以与boost::fusion for_each
一起实现,但我需要短路行为,一旦其中一个处理程序返回true,则不调用其余处理程序。
你可以尝试这样做:
void my_eval() {}
template<typename T, typename... Ts>
void my_eval(T& t, Ts&... ts) {
if(!t.handle(/* x */))
my_eval(ts...);
}
template<int... Indices>
struct indices {
using next_increment = indices<Indices..., sizeof...(Indices)>;
};
template<int N>
struct build_indices {
using type = typename build_indices<N - 1>::type::next_increment;
};
template<>
struct build_indices<0> {
using type = indices<>;
};
template<typename Tuple, int... Indices>
void my_call(Tuple& tuple, indices<Indices...>) {
my_eval(std::get<Indices>(tuple)...);
}
template<typename Tuple>
void my_call(Tuple& tuple) {
my_call(tuple, typename build_indices<std::tuple_size<Tuple>::value>::type());
}
要使用,只需将元组传递给my_call
简单的模板递归应该生成一个合适的尾部调用函数链。
template< typename head, typename ... rest >
void apply_handler_step( thing const &arg, std::tuple< head, rest ... > * ) {
if ( ! handler< head >( arg ) ) {
return apply_handler_step( arg, (std::tuple< rest ... >*)nullptr );
} // if handler returns true, then short circuit.
}
void apply_handler_step( thing const &arg, std::tuple<> * ) {
throw no_handler_error();
}
如果你想把函数指针指向元组中的处理程序,那么你需要递归一个索引值并使用get< n >( handler_tuple )
,但原理是一样的。
相关文章:
- 在 c++11 中迭代函子元组
- 迭代一组和擦除元素
- 用std :: apply迭代元组元素
- C :优雅地迭代一组数字
- 在 C++17/20 中迭代元组
- 正在对元组进行迭代..再一次
- 在元组中迭代和调用异构函数
- 从向量的 std::元组中迭代选定的向量
- 我可以用openmp迭代C++11 std::元组吗
- 如何对元组进行迭代
- 如何在表达式中迭代命名组
- 可变模板形参和元组迭代
- Boost c++ -动态迭代多数组
- 为不使用元组的自定义映射模拟std::map迭代器
- 迭代一个n元组
- 在c++中对列表中的一对(或元组)的特定项进行迭代
- 如何在c++映射中迭代一组特定的键
- 一元函数,接受迭代器元组并产生基本类型
- 迭代器元组.operator++的实现
- c++ 0x元组没有迭代器