S
Simon Elliott
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>.
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>.