为特定的迭代器类型重载for_each [英] Overloading for_each for specific iterator types

查看:180
本文介绍了为特定的迭代器类型重载for_each的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用typedef在我的程序中定义容器的类型,这样我可以很容易地在使用正常的STL容器和 STXXL 容器,沿着以下行:

I'm using a typedef to define the type of a container in my program so that I can easily switch between using normal STL containers and STXXL containers, along the lines of:

typedef stxxl:vector<Data> MyContainer;

typedef std:vector<Data> MyContainer;

一个困难是STXXL提供了一个特殊版本的 std :: for_each stxxl :: for_each ,用于STXXL容器。我更喜欢使用这个函数时MyContainer类型为 stxxl :: vector

One difficulty is that STXXL provides a special version of std::for_each, stxxl::for_each that is optimised for use with STXXL containers. I'd prefer to use this function when MyContainer is typedeffed as a stxxl::vector.

一个解决方案定义我自己的 for_each 函数调用正确的 for_each 函数,并且每当我想调用 for_each

One solution would be to define my own for_each function that calls the right for_each function and use that whenever I want to call for_each.

我目前正在调查的另一个解决方案是重载/专门化 std :: foreach code> stxxl :: vector< Data>::( const_)iterator 作为第一个调用时,code> stxxl :: for_each 和第二个参数。

Another solution that I'm currently investigating is to overload/specialize std::foreach so that it calls stxxl::for_each whenever it is called with a stxxl::vector<Data>::(const_)iterator as first and second argument.

我不能得到第二个想法工作。我试过以下:

I cannot get the second idea to work though. I've tried the following:

namespace std
{
    template <class UnaryFunction>
    UnaryFunction for_each(stxxl:vector<Data>::const_iterator first, 
        stxxl:vector<Data>::const_iterator last, UnaryFunction f)
    {
        stxxl::for_each(first, last, f, 4);
    }
}

与非const迭代器类似的函数。他们不会被调用。

Along with a similar function for non-const iterators. They don't get called though.

这个问题的首选解决方案是什么?我如何得到我的版本的 std :: for_each stxxl :: vector 迭代器来调用?

What would be the preferred solution to this problem? How can I get my version of std::for_each for stxxl::vector iterators to get called?

更新:我得到了第二个想法,现在工作,发布。问题是,我包括错误的文件(ouch ...)。第一个问题仍然存在:这个问题的首选解决方案是什么?

Update: I got the second idea to work now, as posted. The problem was that I was including the wrong file (ouch...). The first question remains though: What is the preferred solution to this problem? Is it okay to overload std::for_each, as the std namespace is not intended for mere mortals?

推荐答案

你可以专门化模板在std(17.4.3.1),但是你不能添加重载。您的定义是一个重载,而不是标准for_each模板的特殊化,在任何情况下,函数不能被部分专门化。所以它是未定义的任何定义在命名空间std可能做你想要的。

You can specialize templates in std (17.4.3.1), but you can't add overloads. Your definition is an overload, not a specialization of the standard for_each template, and in any case functions can't be partially specialized. So it's undefined to put any definition in namespace std that might do what you want.

ADL应该使这项工作顺利,没有任何需要。我假设stxxl迭代器在stxxl命名空间,所以 for_each(first,last,f,4); 应该调用 stxxl :: for_each 。如果你想要 std :: for_each ,你在调用它时就完全限定了名称。

ADL is supposed to make this work smoothly without any need for that, though. I assume the stxxl iterators are in the stxxl namespace, so for_each(first, last, f, 4); should call stxxl::for_each. If you want std::for_each, you fully qualify the name when you call it.

这篇关于为特定的迭代器类型重载for_each的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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