Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Converting iterator to pointers, revisited

Reply
Thread Tools

Converting iterator to pointers, revisited

 
 
Simon Elliott
Guest
Posts: n/a
 
      01-12-2006
I have some code where it's necessary to convert an iterator to a
pointer, to pass the pointer to a legacy function. I've been looking at
ways of encapsulating this, so I put together a short test app:

#include <vector>
#include <iostream>

int main (int argc, char *argv[])
{
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
std::vector<int>::iterator ii = vi.begin();
int* pi = &*ii;
std::cout << "result:" << *pi << std::endl;
return(0);
}

I want to document and encapsulate the iterator to pointer conversion,
along these lines:

pi = iterator_to_pointer(ii);

*** Attempt#1

#define iterator_to_pointer(i) &*i

This works but it's an Evil Macro. (It should probably be parenthesised
a bit better but I don't want to go down this route so I haven't
bothered.)


*** Attempt#2

int* iterator_to_pointer(std::vector<int>::iterator i)
{
return(&*(i));
}

This works but it's insufficiently generic to be useful. The obvious
way forward is to make this a template:


*** Attempt#3

template<class T> typename T::value_type* iterator_to_pointer(typename
T::iterator i)
{
return(&*(i));
}

This template isn't matched by my line
pi = iterator_to_pointer(ii);

In g++: error: no matching function for call to
`iterator_to_pointer(__gnu_cxx::__normal_iterator< int*,
std::vector<int, std::allocator<int> > >&)'

I can't work out why this isn't matched. Any ideas?

BTW, I expect iterator_to_pointer() not to work with oddities such as
std::vector<bool> but it would be a nice bonus if it wouldn't compile
if someone tries to use it with std::vector<bool>.

--
Simon Elliott http://www.ctsn.co.uk
 
Reply With Quote
 
 
 
 
=?iso-8859-2?Q?Ale=b9=20Pergl?=
Guest
Posts: n/a
 
      01-12-2006
> I have some code where it's necessary to convert an iterator to a
> pointer, to pass the pointer to a legacy function.
> ...
> std::vector<int>::iterator ii = vi.begin();
> int* pi = &*ii;


This is not a conversion of any sort. You are merely obtaining a reference
to the item in the vector that the iterator is positioned at and then getting
the address of that item and assigning it to pi. That's quite a normal way
of doing this kind of thing so I wouldn't bother with creating a specific
function for it.

> *** Attempt#3
>
> template<class T> typename T::value_type* iterator_to_pointer(typename
> T::iterator i)
> {
> return(&*(i));
> }
> ...
> In g++: error: no matching function for call to
> `iterator_to_pointer(__gnu_cxx::__normal_iterator< int*,
> std::vector<int, std::allocator<int> > >&)'


The compiler is unable to deduce the template parameter T from what you are
providing as an argument to the function. You are providing vector<int>::iterator
but the compiler cannot figure out that you want T to be vector<int>. If
you really want to create a function like this (discouraged), do it like
this:

template<class T> typename T:ointer iterator_to_pointer(T& i)
{
return(&*(i));
}


 
Reply With Quote
 
 
 
 
fluidparody@yahoo.com
Guest
Posts: n/a
 
      01-12-2006
This one seems to work:

#include <iostream>
#include <vector>

template <typename T, typename I>
T* iterator_to_pointer(I i)
{
return (&(*i));
}

int main()
{
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
std::vector<int>::iterator ii = vi.begin();

int *pi = iterator_to_pointer<int, std::vector<int>::iterator >
(ii);

std::cout << *pi << std::endl;

return 0;
}

 
Reply With Quote
 
Simon Elliott
Guest
Posts: n/a
 
      01-13-2006
On 12/01/2006, Aleš Pergl wrote:

> > std::vector<int>::iterator ii = vi.begin();
> > int* pi = &*ii;

>
> This is not a conversion of any sort. You are merely obtaining a
> reference to the item in the vector that the iterator is positioned
> at and then getting the address of that item and assigning it to pi.
> That's quite a normal way of doing this kind of thing so I wouldn't
> bother with creating a specific function for it.


Fair enough. I want to reduce the maintenace burden, but if it's fairly
commonplace and well understood, perhaps I won't bother.

> The compiler is unable to deduce the template parameter T from what
> you are providing as an argument to the function. You are providing
> vector<int>::iterator but the compiler cannot figure out that you
> want T to be vector<int>. If you really want to create a function
> like this (discouraged), do it like this:
>
> template<class T> typename T:ointer iterator_to_pointer(T& i)
> {
> return(&*(i));
> }


The iterator:ointer type seems to be patchily documented (eg it's not
in the docs for my compiler, BCB6). Is it a fairly new/non standard
feature?

--
Simon Elliott http://www.ctsn.co.uk
 
Reply With Quote
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-13-2006
Simon Elliott wrote:

>
> The iterator:ointer type seems to be patchily documented (eg it's not
> in the docs for my compiler, BCB6). Is it a fairly new/non standard
> feature?
>
> --
> Simon Elliott http://www.ctsn.co.uk


IIRC from Bjarne Stroustrup iterators are implementation specific
(thus the surprising error message in the original post):
you only can rely on their semantics.

Regards, Stephan
http://www.velocityreviews.com/forums/(E-Mail Removed)
Open source rating and billing engine for communication networks.

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      01-14-2006

"Simon Elliott" <Simon at ctsn.co.uk> skrev i meddelandet
news:43c7d601$0$87299$(E-Mail Removed).. .
>>
>> template<class T> typename T:ointer iterator_to_pointer(T& i)
>> {
>> return(&*(i));
>> }

>
> The iterator:ointer type seems to be patchily documented (eg it's
> not
> in the docs for my compiler, BCB6). Is it a fairly new/non standard
> feature?


The formally correct way to access the pointer type is

typename std::iterator_traits<T>:ointer

which might be equivalent to T:ointer, but doesn't have to be.


Bo Persson


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments