vector::iterator to a pointer casting

R

Raider

Why it's impossible for compiler to implicitly cast vector<>::iterator
to void*?

I have:
void f(void *, size_t);
....
vector<int> v;
v.reserve(n);
f(v.begin(), n * sizeof(int));

And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What's the easiest way to do it? &(*v.begin())?
 
M

Markus Moll

Hi
Why it's impossible for compiler to implicitly cast vector<>::iterator
to void*?

Because vector said:
And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What's the easiest way to do it? &(*v.begin())?

It would work (unless v.size() == 0, i.e. v.begin() == v.end()), but I think
it's easier and more common to use &v[0].

Markus
 
B

benben

Raider said:
Why it's impossible for compiler to implicitly cast vector<>::iterator
to void*?

I have:
void f(void *, size_t);
...
vector<int> v;
v.reserve(n);
f(v.begin(), n * sizeof(int));

And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What's the easiest way to do it? &(*v.begin())?

You will find out that C++ generally does not allow you to implicitly
cast just about any type to void*, period.

But if you insist, you can do it the following rather ugly code:

void* pt = static_cast<void*>(&v[0]);

Regards,
Ben
 
B

Ben Pope

Raider said:
Why it's impossible for compiler to implicitly cast vector<>::iterator
to void*?

because vector<>::iterator is not a pointer.

That's like asking why it can't convert the vector to a void pointer.

The iterator has pointer syntax, and much of pointer semantics, but that
doesn't mean it IS a pointer.
I have:
void f(void *, size_t);
...
vector<int> v;
v.reserve(n);
f(v.begin(), n * sizeof(int));

And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What's the easiest way to do it? &(*v.begin())?

Yeah.

Ben Pope
 
R

Raider

That's like asking why it can't convert the vector to a void pointer.

I thought that something like operator T*() exists for
vector<T>::iterator.

Thanks!
 
B

Ben Pope

Raider said:
I thought that something like operator T*() exists for
vector<T>::iterator.

You'd probably get away with it for vector<T>::iterator, but not all
iterators support everything that a pointer does, so it would introduce
an asymmetry between say, vector and list.

Ben Pope
 
J

Jeff Flinn

Raider said:
I have v.reserve(n) to avoid this ;-)

reserve affects the capacity, not the size. ;-) Taking &v[0] when v.empty()
at best invokes undefined behaviour.

Jeff Flinn
 
R

Raider

SGI says (btw, where can I find C++ standard?) for STL vector:
void reserve(size_type n)
If n is less than or equal to capacity(), this call has no effect.
Otherwise, it is a request for allocation of additional memory
http://www.sgi.com/tech/stl/Vector.html

Ok, size() == 0 but I think capacity() will be enough to use v[0] up to
v[n-1]. Is it needs to use resize()??? I don't think so.
 
C

Clark S. Cox III

I thought that something like operator T*() exists for
vector<T>::iterator.

No, iterators are not necessarily convertible to pointers. If you've
been relying on this behavior, then your code is broken (i.e. it may
work with some compilers, but not with others).
 
A

Andre Kostur

SGI says (btw, where can I find C++ standard?) for STL vector:
void reserve(size_type n)

See the FAQ (http://www.parashift.com/c++-faq-lite), section 6.13.
If n is less than or equal to capacity(), this call has no effect.
Otherwise, it is a request for allocation of additional memory
http://www.sgi.com/tech/stl/Vector.html

Allocates memory, but doesn't mean that the memory contains valid objects.
Ok, size() == 0 but I think capacity() will be enough to use v[0] up to
v[n-1]. Is it needs to use resize()??? I don't think so.

Yes, you must use resize. While you might be lucky, and it may work for a
vector of ints, it won't work for vector of class (like std::string). Also
if you're using a checked/debug version of the STL, it may enforce this as
well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top