I'm looking through some c++ wrapper code that provides a c api, and I'm finding lots of reinterpret_cast
where a static_cast
would suffice, e.g.:
struct cpp_object{ void foo(){ /* do something */ } };
/* begin: c api */
typedef void c_object;
void foo(c_object *o)
{
// why this:
reinterpret_cast<cpp_object *>(o)->foo();
// instead of just:
static_cast<cpp_object *>(o)->foo();
}
/* end: c api */
Generally I use reinterpret_cast
in rare situations, mostly related to forced bit coersion of buffer contents to a type of know layout and size, known to lie inside buffer contents.
So I ask whether that practice makes sense or sticking to static_cast
would be a better one.