Boost logo

Boost :

From: Gabriel Dos Reis (gdr_at_[hidden])
Date: 2002-12-07 12:12:03


"Eric Woodruff" <Eric.Woodruff_at_[hidden]> writes:

| "Gabriel Dos Reis" <gdr_at_[hidden]> wrote in message
| news:m34r9qjcep.fsf_at_uniton.integrable-solutions.net...
| > "Eric Woodruff" <Eric.Woodruff_at_[hidden]> writes:
| >
| > [...]
| >
| > | > Thus, given that h.storage is properly aligned, (which is the purpose
| of
| > | the
| > | > other union member), after "new(h.storage) Foo", h.storage contains a
| Foo
| > | > object. Thus accessing it through a pointer-to-Foo is legal, as Foo is
| the
| > | > dynamic type of the object.
| > | >
| > |
| > | This is precisely my reasoning why reinterpret_cast<> is _not_
| > | implementation defined.
| > |
| > | It must be the case that it is equal to the situation of having
| > |
| > | Foo* -> char */void* -> Foo* if Foo is the dynamic type of the object.
| >
| >
| > After giving more thoughts to the analysis given by Anthony and
| > 5.2.10/1, I think I agree with you're right.
| >
|
| Well, for a reinterpret_cast to be valid, it must be used to cast away from
| the type, and return to it.

Not necessarily: It is permitted to have the source type be
identitical to the target type. But that is a minor nit.

| In my example, I was only returning to it. I
| question the validity of that. If new (p) Object; takes a void* and does an
| implicit cast, I don't think there is any reinterpret_casting taking place
| on the inside.

Certainly, there is no reinterpret_cast<> happening here. However,
the result of reinterpret_cast<>ing a Foo* to void* is the result of
converting a Foo* to void* (that is what I understand from 5.2.10/1),
therefore the two happen to have the same value there.

| However, I think there must be some kind of cast to char* to
| allocate the memory in number of bytes, but I'm not sure. The size of char
| is not necessarily 1 byte, correct? So either way, this allocation
| implementation defines what happens.

I'm afraid I don't undertand this part. Could you elaborate? What do
you mean by "The size of char is not necessarily 1 byte".

| Again, however, I mentioned in a different post that we could always force a
| reinterpret_cast<> via new (reinterpret_cast<void*> (h.storage)) Object, but
| if an implementation remembers that h.storage is an unsigned char*, or even
| if it doesn't, I think reinterpret_cast may only be valid to cast void*
| h.storage to unsigned char* h.storage.

Anthony convincingly showed that provided

  (1) h.storage is properly aligned
  (2) an object is properly constructed at h.storage

the type rules allows one to reinterpret_cast<>/static_cast<> to a
Foo* -- even an implementation that monitors memory access ought to
to accept that usage.

Or do you think I misundertstand your point?

-- Gaby


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk