Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: template er...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 8 Topic 45888 of 47017
Post > Topic >>

Re: template error with iterator

by James Kanze <james.kanze@[EMAIL PROTECTED] > May 13, 2008 at 02:26 AM

On May 13, 1:57 am, Paavo Helde <nob...@[EMAIL PROTECTED]
> wrote:
> utab <umut.ta...@[EMAIL PROTECTED]
> wrote in news:50129083-18fd-4099-9ba7-
> 586bb9df7...@[EMAIL PROTECTED]
> > I was experimenting with a template taking two iterators for
> > the range of a vector.(Perhaps, it is sth simple and I am
> > missing it because it is a late hour.) I ran into problems
> > in the compile phase , the code is below:

    [...]
> .. or throw away the redundant template parameter:

> template <class Ran>
> typename Ran::value_type median(Ran b, Ran e)

There's no guarantee that Ran has a typedef for value_type (and
there have definitely been implementations of the standard
library where it didn't for std::vector<>).

This should be:

    template< typename RandomAccessIterator >
    typename std::iterator_traits< RandomAccessIterator >::value_type
    median( RandomAccessIterator begin, RandomAccessIterator end )

> {
>   typedef typename vector<Ran::value_type>::size_type vec_sz;

And here, even worse, since you're tying yourself to
std::vector.  (Even as it stands, you need an additional
typename in the template argument.)  Also, the difference
between to random access iterators is a difference_type, not a
size_type.  So it should be:

    typedef typename std::iterator_traits< RandomAccessIterator >
                ::difference_type SequenceSize ;

>   vec_sz size =3D (e-b)/sizeof(Ran::value_type);

And of course, you don't want the division.

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 8 Posts in Topic:
template error with iterator
utab <umut.tabak@[EMAI  2008-05-12 16:37:07 
Re: template error with iterator
utab <umut.tabak@[EMAI  2008-05-12 16:40:04 
Re: template error with iterator
tragomaskhalos <dave.d  2008-05-12 16:47:35 
Re: template error with iterator
utab <umut.tabak@[EMAI  2008-05-12 16:51:26 
Re: template error with iterator
acehreli@[EMAIL PROTECTED  2008-05-12 16:52:42 
Re: template error with iterator
Paavo Helde <nobody@[E  2008-05-12 18:57:09 
Re: template error with iterator
Greg Herlihy <greghe@[  2008-05-12 16:58:41 
Re: template error with iterator
James Kanze <james.kan  2008-05-13 02:26:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 16:15:28 CDT 2008.