For_each overload for specific types of iterators

I use typedef to determine the type of container in my program so that I can easily switch between regular STL and STXXL containers , according to:

typedef stxxl:vector<Data> MyContainer;

or

typedef std:vector<Data> MyContainer;

One of the difficulties lies in the fact that STXXL provides a special version std::for_each, stxxl::for_eachoptimized for use with STXXL containers. I would prefer to use this function when MyContainer is entered as stxxl::vector.

One solution would be to define my own function for_eachthat calls the correct function for_eachand use it whenever I want to call for_each.

Another solution that I am currently studying is overload / specialization std::foreachso that stxxl::for_eachit invokes whenever it is called with stxxl::vector<Data>::(const_)iteratorboth the first and second arguments.

I can not get the second idea to work. I 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);
    }
}

Along with a similar function for non-constant iterators. They are not called though.

What would be the preferred solution to this problem? How can I get my version std::for_eachfor stxxl::vectoriterators to call?

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

+3
5

std (17.4.3.1), . - , for_each, . , undefined std, , .

ADL - . , stxxl stxxl, for_each(first, last, f, 4); stxxl::for_each. std::for_each, .

+6

, std, , . std::swap , USENET .

, , std, - . , :

namespace std {
    template <>
    MyFunction for_each(stxxl::vector<Data>::const_iterator first,
                        stxxl::vector<Data>::const_iterator last,
                        MyFunction func)
    {
        return stxxl::for_each(first, last, func);
    }
}

, . , . , USENET. . , (gasp), , /.

ADL, , , stxxl std typedef. , , , .

for_each, . , , .

+2

std:: for_each - , , , - , , ; , for_each

, , std:: for_each .

+1

:

  • .
  • std
  • , , - for_each ( "" ), . , ADL .
+1

std:: for_each().
, .

, std:: for_each(), stlxxl.
stlxxl, , , .

#include <vector>
#include <list>
#include <algorithm>
#include <iostream>

//
// Normal version.
template<typename U,typename T,typename A,template<class I,class A> class C>
U Ufor_each(C<T,A>& cont,U const& f)
{
    std::cout << "NORMAL Start" << std::endl;
    return std::for_each(cont.begin(),cont.end(),f);
    return f;
}

// 
// Specialized version.
// You could write a specialized version for stlxxl::vector
template<typename U,typename T>
U Ufor_each(std::vector<T>& cont,U const& f)
{
    std::cout << "Vector Start" << std::endl;
    return std::for_each(cont.begin(),cont.end(),f);
}

struct X
{
    void operator()(int data) const
    {
        std::cout << "Item: " << data << std::endl;
    }
};

int main()
{
    std::vector<int>    vect;
    std::list<int>      list;

    vect.push_back(1);
    list.push_back(4);

    X                   x;
    Ufor_each(vect,x);
    Ufor_each(list,x);
}

, .

0

Source: https://habr.com/ru/post/1727035/


All Articles