   Question about a code in a book
  I just started reading C++ metaprogramming, and its just in the beginning. I havea quick naive question. Right now they're using the metafunction itr_swap asa way to teach some metaprogramming things. So the final implementation they came up with is something like this :  #include <boost/type_traits/is_reference.hpp>  #include <boost/type_traits/is_same.hpp>  #include <iterator> // for iterator_traits  #include <utility> // for swap  template <bool use_swap> struct iter_swap_impl; // see textnamespace std {  template <class ForwardIterator1, class ForwardIterator2>  void iter_swap(ForwardIterator1 i1, ForwardIterator2 i2){ typedef iterator_traits<ForwardIterator1> traits1; typedef typename traits1::value_type v1; typedef typename traits1::reference r1; typedef iterator_traits<ForwardIterator2> traits2; typedef typename traits2::value_type v2; typedef typename traits2::reference r2; bool const use_swap = boost::is_same<v1,v2>::value && boost::is_reference<r1>::value && boost::is_reference<r2>::value;//...//...}template <>struct iter_swap_impl<true> // the "fast" one{  template <class ForwardIterator1, class ForwardIterator2>  static void do_it(ForwardIterator1 i1, ForwardIterator2 i2){std::swap(*i1, *i2);    }};template <>struct iter_swap_impl<false> // the one that always works{template <class ForwardIterator1, class ForwardIterator2>static void do_it(ForwardIterator1 i1, ForwardIterator2 i2){ typename iterator_traits<ForwardIterator1>::value_type tmp = *i1; *i1 = *i2; *i2 = tmp; }};The part thats confusing me is this part, the call :iter_swap_impl<use_swap>::do_it(*i1,*i2);The method do_it is expecting a iterator, so it can dereference it, why is theauthor passing in a explicit value? I must be thinking something wrong.   
  What is the type of i1 and i2? Can you post the code that makes the call?   ;  They are both type ForwardIterator, which is a template type that you can use with any iterator that has the normal forward iteration functionality (like ++ for iterating forward, as well as being able to grab the reference of what it has iterated to with *).   ;Quote:Original post by Gage64What is the type of i1 and i2? Can you post the code that makes the call?It doesn't specify. I assume its the same i1 and i2, from the parameters of do_it(...);   ;  For what it is worth and I quote:"IMPL is a means to get from an interface pointer to an instance"mmmmm... ok?Maybe that is what it impl...ies. sorry no help at all...maybe it just means implementation?   ;  Since the swap wants to swap the values, not iterators.struct SimpleIterator {  Value & operator*();};SimpleIterator a,b;Value x,y;swap(a, b); // swaps iteratorsswap(*a, *b); // swap values, as provided by reference via iteratorx = *a;y = *b;swap(x, y);;Quote:Original post by AntheusSince the swap wants to swap the values, not iterators.struct SimpleIterator {  Value & operator*();};SimpleIterator a,b;Value x,y;swap(a, b); // swaps iteratorsswap(*a, *b); // swap values, as provided by reference via iteratorx = *a;y = *b;swap(x, y);But the do_it function deferences the iterator to swap the values of the two iterators.   ;Quote:Original post by ConcentrateBut the do_it function deferences the iterator to swap the values of the two iterators.So you have the implementation which you showed and then this line of code:iter_swap_impl<use_swap>::do_it(*i1,*i2); which doesn't define i1 and i2. if i1 and i2 are iterators then that code will only work if after de-referencing them their value resolves to another iterator. I find it highly unlikely that was intended by the author. This means either the do_it function is written wrong or that final line of code you showed is wrong.I believe this was the intended way to call the function:ForwardIterator i1, i2// ...iter_swap_impl<use_swap>::do_it(i1, i2);Books usually contain errors and my guess is that this is the case here.   