Boost logo

Boost :

From: Gabriel Dos Reis (gdr_at_[hidden])
Date: 2002-12-04 13:39:38


David Abrahams <dave_at_[hidden]> writes:

| Gabriel Dos Reis <gdr_at_[hidden]> writes:
|
| > David Abrahams <dave_at_[hidden]> writes:
| >
| > | Gabriel Dos Reis <gdr_at_[hidden]> writes:
| > |
| > | > "David B. Held" <dheld_at_[hidden]> writes:
| > | >
| > | > | Does this have any merit whatsoever?
| > | > |
| > | > | template <typename T, typename U>
| > | > | T* dangerous_cast(U* p)
| > | > | {
| > | > | return static_cast<T*>(static_cast<void*>(p));
| > | > | }
| > | >
| > | > Is it any different from reinterpret_cast<T*>(p) ?
| > |
| > | It might be, depending on your compiler. The behavior of
| > | reinterpret_cast<T*> is implementation-defined.
| >
| > The mapping used by reinterpret_cast<> is implementation defined,
| > thus an implementation is required to document it.
| >
| > 1) Do you have any corresponding guarantee for converting a void* to
| > T* when the source is *not* a T* nor the address obtained from a T?
| >
| > 2) Isn't the mapping U* -> void* -> T* at best
| > implementation-defined if not undefined behaviour?
|
| template <class T>
| struct holder
| {
| union {
| aligned_POD<T>::type aligner;
| char storage[sizeof(T)];
| };
| };
|
| holder<Foo> h;
| new (h.storage) Foo;
| ...
| T* u = dangerous_cast<T*>(h.storage); // can't do this with reinterpret_cast

Hmm, I have a couple of questions answers to which will help me
get your point.

 1) Why can't you do that with reinterpret_cast?
 2) Why is that supposed to be better than reinterpret_cast<>?

-- Gaby


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