过滤模板函数的特定迭代器 [英] Filtering specific iterators for template functions

查看:107
本文介绍了过滤模板函数的特定迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一组函数,这些函数利用了具有打包和顺序内存存储的容器(用于内存副本)。它们具有大多数STD函数样式的函数签名,输入/输出迭代器指向元素并表示范围。例如,函数可能如下所示:

I am developing a set of functions that takes advantage of containers that have packed and sequential memory storage (for memory copies). They have function signatures in the style of most STD functions, input/output iterators point to elements and denote ranges. For instance, a function could look like this:

template< typename InputIterator, typename OutputIterator >
OutputIterator fooBar( InputIterator& first, InputIterator& last, 
                       OutputIterator& result );

我希望验证传递的迭代器是合法的,是打包的还是顺序的。对于STD容器,这仅限于std :: vector和std :: array。不幸的是,我不能依赖迭代器类别特征,因为随机访问特征并不意味着存储。这方面的一个例子是microsofts concurrent_vector类,这里记录了并行容器

I wish to verify that the iterators passed are legal, that is packed and sequential. For the STD containers, this is limited to std::vector and std::array. Unfortunately, I can't rely on the iterator 'category' trait, because the random access trait does not imply seqential storage. An example of this is microsofts concurrent_vector class, documented here parallel containers

此外,我无法接受来自向量和数组类的所有迭代器,例如我需要拒绝反向迭代器和std :: vector < bool >迭代器因其使用的代理类而不适合。

In addition, I can't accept all iterators from the vector and array classes either, for instance i need to reject reverse iterators, and std::vector<bool> iterators are unsuitable because of the proxy class that it uses.

我试图创建自己的traits类来区分和过滤迭代器与我上面描述的约束,但我遇到了模板语法问题。我正在寻找其他人如何解决这个问题的反馈。

I've attempted to create my own traits class to distinguish and filter the iterators with the constraints that i describe above, but i'm running into template syntax problems. I am looking for feedback from others on how they would approach this problem.

谢谢

推荐答案

我认为你不能这样做。迭代器是一种抽象,其完整目的是使迭代过程独立于底层架构。标准迭代器中没有信息表示底层内存结构甚至是远程类似的东西。

I don't think you can do this. Iterators are an abstraction whose whole purpose is to make the iteration process independent of the underlying architecture. There is no information in the standard iterators that denote the underlying memory structure or even anything remotely similar.

在类似std算法的函数中,通常建议传递迭代器按价值,因为它们应该是便宜/小物件。应特别注意的是,您的函数永远不能被称为 fooBar(c.begin(),c.end(),some_out_it); ,因为它需要通过引用到非const的输入迭代器。

On your std-algorithm-like functions, it's generally advised to pass iterators by value, since they should be cheap / small objects. It should be especially noted that your function would never be able to be called as fooBar(c.begin(), c.end(), some_out_it);, since it takes the the input iterators by reference-to-non-const.

作为最后一点,您可以通过测试迭代器类型是否为<的特化来过滤掉反向迭代器。 code> std :: reverse_iterator< Iter> ,因为至少容器::( const_)reverse_iterator 标准容器的类型是必需的是一个。

As a last point, you can filter out reverse iterators by testing whether the iterator type is a specialization of std::reverse_iterator<Iter>, since atleast the Container::(const_)reverse_iterator type of standard containers are required to be one.

这篇关于过滤模板函数的特定迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆