Boost logo

Boost Users :

Subject: Re: [Boost-users] [graph] [beginner] How to use the "tie(...) feature" with standard c++ containers?
From: Christoph (c_p_at_[hidden])
Date: 2011-10-10 05:04:38


[...]
> You need to use boost::tie if your iterator types are not already in the
> boost namespace. The definition of tie is in Boost.Tuple, I think.

Thank you Jeremiah for pointing that out. I think i still have my
problems with ADL and name spaces...
Off course ADL (Argument Dependant Name Lookup) will not find any
definition for 'tie' with the arguments being in the name space 'std',
because 'tie' is defined in the name space 'boost' and not in the name
space 'std'.

So here is an unhappy example in which i write 'boost::tie' in stead of
just 'tie' which compiles without errors but does not do what i
expected:

// begin code

#include <iostream>
#include <boost/graph/adjacency_list.hpp>

typedef boost::adjacency_list < boost::listS, boost::vecS,
boost::bidirectionalS>
my_graph;

typedef boost::graph_traits <my_graph>
my_graph_traits;

template <typename T>
boost::tuple < typename T::iterator, typename T::iterator>
elements (T container)
{
        typename T::iterator beg = container.begin();
        typename T::iterator end = container.end();
        for (;beg != end; ++beg)
                std::cout << *beg << ": loop in elements()" << std::endl;
        return boost::make_tuple(container.begin(), container.end());
}

int main ()
{
        std::list <int> list;
        list.push_back(1);list.push_back(2);
        list.push_back(3);list.push_back(4);
        
        std::list <int>::iterator beg, end;
        int i = 0;
        
/* uncomment for testing purpose
        for (boost::tie(beg, end) = elements <std::list<int> >(list); beg !=
end; ++beg)
        {
                std::cout << *beg << ", i = " << i++ << std::endl;
        }*/
        
        beg = list.begin();
        end = list.end();
        for(; beg != end; ++beg)
                std::cout << *beg << ", i = " << i++ << std::endl;
        return 0;
}
// end code

If you compile and run the code as it is the output is:
1, i = 0
2, i = 1
3, i = 2
4, i = 3

However if you uncomment the 'for-loop' part and leave the rest of the
code as it is, compile and run, than the output will be:
1: loop in elements()
2: loop in elements()
3: loop in elements()
4: loop in elements()
1, i = 0

I expected the output to be:

1: loop in elements()
2: loop in elements()
3: loop in elements()
4: loop in elements()
1, i = 0
2, i = 1
3, i = 2
4, i = 3
1, i = 0
2, i = 1
3, i = 2
4, i = 3

I do not get it. Maybe you could help me to find out.

best regards
again Christoph


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net