Boost logo

Boost Users :

Subject: Re: [Boost-users] [graph] [beginner] How to use the "tie(...) feature" with standard c++ containers?
From: Cedric Laczny (cedric.laczny_at_[hidden])
Date: 2011-10-10 05:29:49


Hi,

On Monday, 10. October 2011 11:04:38 Christoph wrote:
> [...]
>
> > 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)

Please note that you are using a _copy_ here and thus you also return the
iterators of a _copy_ but not of the "original" container. Therefore, I assume
that the iterators returned are not valid and might even lead to a
segmentation fault (as they do on my machine, since this container-copy is
only valid during the execution of elements() ).
Perhaps you want to try "const T& container" in the argument list.
When I use that, I get:
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 = 4
2, i = 5
3, i = 6
4, i = 7

> {
> 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,

Cedric

> best regards
> again Christoph
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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