Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Understanding an implementation of uninitialized_copy

23 views
Skip to first unread message

Paul

unread,
Mar 13, 2015, 5:34:46 AM3/13/15
to
I don't completely understand the implementation from http://www.cplusplus.com/reference/memory/uninitialized_copy/ although I do understand the basic idea.

The function is implemented below. I don't understand why (&*result) instead of just (result). Surely, when you dereference a pointer and then take the address, the operations just reverse each other. Using (result) instead of (&*result) worked fine on my compiler so I wasn't able to see why this was necessary. I also don't see why casting to void* is necessary. This also wasn't necessary on my compiler.

Thank you,

Paul

template<class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_copy ( InputIterator first, InputIterator last,
ForwardIterator result )
{
for (; first!=last; ++result, ++first)
new (static_cast<void*>(&*result))
typename iterator_traits<ForwardIterator>::value_type(*first);
return result;
}

fefe

unread,
Mar 13, 2015, 5:46:12 AM3/13/15
to
As implied by the name of the template parameter `ForwardIterator`, it may not be a pointer but an iterator, such as vector::iterator. Most iterators are not pointers, so *result will the object, and &*result will get the address of the object the iterator points to.
0 new messages