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

difference &*ptr and ptr

0 views
Skip to first unread message

Lee Chang Ha

unread,
Apr 7, 2003, 9:23:17 AM4/7/03
to
Hi friends.. :)

Below code is part of mojo(by andrei alexandrescu)
I want to know the difference between new(&*built) and just new(built)..
^^^^^^^ ^^^^^
<part of mojo>

template <class Iter1, class Iter2>
Iter2
uninitialized_move(Iter1 begin, Iter1 end, Iter2 dest)
{
typedef typename std::iterator_traits<Iter2>::value_type T;
Iter2 built = dest;
try {
for (; begin != end; ++begin, ++built) {
new (&*built) T(as_temporary(*begin));
^^^^^^^^^^^^^
}
} catch (...) {
for (; dest != built; ++dest) {
dest->~T();
}
throw;
}
return built;
}

thanks in advance

Yi Chang Ha

Rolf Magnus

unread,
Apr 7, 2003, 9:28:38 AM4/7/03
to
Lee Chang Ha wrote:

> Hi friends.. :)
>
> Below code is part of mojo(by andrei alexandrescu)
> I want to know the difference between new(&*built) and just
> new(built)..
> ^^^^^^^ ^^^^^

It depends on what 'built' is. If it's a pointer, there is no
difference. But in your example, it's an iterator. If you want a
pointer to the element an iterator refers to, you first need to
derererence the iterator to get the element itself and then use the
address operator to get a pointer to it. That's why &*built is used
instead of just built.

jinfeng_Wang

unread,
Apr 8, 2003, 9:36:12 AM4/8/03
to

"Lee Chang Ha" <le...@ovytz.com>
??????:a0c361fc.0304...@posting.google.com...

> Hi friends.. :)
>
> Below code is part of mojo(by andrei alexandrescu)
> I want to know the difference between new(&*built) and just new(built)..
> ^^^^^^^ ^^^^^
> <part of mojo>
>
> template <class Iter1, class Iter2>
> Iter2
> uninitialized_move(Iter1 begin, Iter1 end, Iter2 dest)
> {
> typedef typename std::iterator_traits<Iter2>::value_type T;
> Iter2 built = dest;
> try {
> for (; begin != end; ++begin, ++built) {
> new (&*built) T(as_temporary(*begin));
> ^^^^^^^^^^^^^
here is the placement new . it is : new (address) T();
that means built one object in the address .
"built" is one iterator . in the stl , iterator is just some concept that
acts like pointer . but iterator can be instantationed as class in which
will difine the operator *() . "&*built " maybe be eaqual to
(built.operator*()).operator&() .

in addition , even built is one pointer, maybe &*build != build .
for example : if build is some base pointer that points to the multiple
virtual derived class .

Lee Chang Ha

unread,
Apr 8, 2003, 10:38:49 PM4/8/03
to
I got it, perfectly..

Thanks again :)

0 new messages