Brian Neal
2006-02-15 13:33:39 UTC
In Item 92 of Sutter and Alexandrescu's C++ Coding Standards book, they
argue you should avoid using reinterpret_cast.
In the case of casting between unrelated pointer types, they recommend
that instead of doing this:
T1* p1 = ...;
T2* p2 = reinterpret_cast<T2*>(p1);
that you do this:
T1* p1 = ...;
void* pv = p1;
T2* p2 = static_cast<T2*>(pv);
I have started to follow this advice, as in the lowest layers of our
code base we have to do this a lot (for example, streaming bytes in/out
of API's beyond our control, message stack processing, etc...all low
level stuff that is carefully hidden from the rest of our software).
I was wondering if it would be useful to introduce a set of functions:
template <class T>
T pointer_cast(void* p)
{
return static_cast<T>(p);
}
template <class T>
T pointer_cast(const void* p)
{
return static_cast<T>(p);
}
template <class T>
T pointer_cast(volatile void* p)
{
return static_cast<T>(p);
}
template <class T>
T pointer_cast(const volatile void* p)
{
return static_cast<T>(p);
}
Then you can say things like:
T1* p1 = ...;
T2* p2 = pointer_cast<T1*>(p1);
Its a bit more notationally cleaner than introducing a stray void*, or
by doing two static_casts on one line:
These functions are a bit safer than doing old-school C-style casts
like T2* p2 = (T2*) p1; since they will still flag changes in const and
volatile as errors.
Could I get some feedback on this please? Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
argue you should avoid using reinterpret_cast.
In the case of casting between unrelated pointer types, they recommend
that instead of doing this:
T1* p1 = ...;
T2* p2 = reinterpret_cast<T2*>(p1);
that you do this:
T1* p1 = ...;
void* pv = p1;
T2* p2 = static_cast<T2*>(pv);
I have started to follow this advice, as in the lowest layers of our
code base we have to do this a lot (for example, streaming bytes in/out
of API's beyond our control, message stack processing, etc...all low
level stuff that is carefully hidden from the rest of our software).
I was wondering if it would be useful to introduce a set of functions:
template <class T>
T pointer_cast(void* p)
{
return static_cast<T>(p);
}
template <class T>
T pointer_cast(const void* p)
{
return static_cast<T>(p);
}
template <class T>
T pointer_cast(volatile void* p)
{
return static_cast<T>(p);
}
template <class T>
T pointer_cast(const volatile void* p)
{
return static_cast<T>(p);
}
Then you can say things like:
T1* p1 = ...;
T2* p2 = pointer_cast<T1*>(p1);
Its a bit more notationally cleaner than introducing a stray void*, or
by doing two static_casts on one line:
These functions are a bit safer than doing old-school C-style casts
like T2* p2 = (T2*) p1; since they will still flag changes in const and
volatile as errors.
Could I get some feedback on this please? Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]