I need a way to get a pointer to the start of an object in C++. This object is used inside a template so it can be any type (polymorphic or not) and could potentially be an object that uses multiple inheritance.
I found this article which describes a way to do it (see the section called "Dynamic Casts") using typeid and a dynamic_cast to void* in the case that T is a polymorphic type.
This works perfectly well on MSVC, however on GCC (4.x) it seems to fall on its arse and spits out a compiler error when it is used with a non-polymorphic type.
Does anyone know a way to:
Below is the code I am currently using to try and achieve this.
template <typename T>
void* dynamicCastToVoidPtr(T *const ptr)
{
// This is done using a separate function to avoid a compiler error on some
// compilers about non-polymorphic types when calling startOfObject
return dynamic_cast<void*>(ptr);
}
template <typename T>
void* startOfObject(T *const ptr)
{
// In cases of multiple inheritance, a pointer may point to an offset within
// another object
// This code uses a dynamic_cast to a void* to ensure that the pointer value
// is the start of an object and not some offset within an object
void *start = static_cast<void*>(ptr);
if(start)
typeid(start = dynamicCastToVoidPtr(ptr), *ptr);
return start;
}
template <typename T>
void doSomethingWithInstance(T *const instance)
{
// Here is where I need to get a void* to the start of the object
// You can think of this as the deleteInstance function of my memory pool
// where the void* passed into freeMemory should point to the
// start of the memory that the memory pool returned previously
void *start = startOfObject(instance);
if(start)
allocator->freeMemory(start);
}
Thanks.
I've found a solution from another question that lets me work out at compile time if a type is polymorphic and I can then use this with template specialisation to use the correct type of cast. Apparently this method might break if the compiler adds padding between sub-objects, but I can hopefully add some compile time asserts on some known cases to catch that. It does compile and run correctly on both MSVC and GCC.
This is the code to work out if a type is polymorphic.
#define SIMPLE_POLYMORPHIC(TYPE, POLYMORPHIC) \
template <> \
struct IsPolymorphic<TYPE> \
{ \
static const bool value = POLYMORPHIC; \
};
template <typename T>
struct IsPolymorphic
{
struct Derived : public T { virtual ~Derived(); };
static const bool value = (sizeof(Derived) == sizeof(T));
};
SIMPLE_POLYMORPHIC(int, false);
SIMPLE_POLYMORPHIC(unsigned int, false);
// ... do this for all intrinsic or non-derivable types
The code to perform the casting based on whether the type is polymorphic.
template <typename T, bool isPolymorphic = IsPolymorphic<T>::value>
struct StartOfObject
{
static void* getStart(T *const ptr)
{
return static_cast<void*>(ptr);
}
};
template <typename T>
struct StartOfObject<T, true>
{
static void* getStart(T *const ptr)
{
if(ptr)
return dynamic_cast<void*>(ptr);
return NULL;
}
};
And a test case for it.
#define CLASS_STUFF(CLASS) \
public: \
CLASS() {} \
virtual ~CLASS() {} \
int m_##CLASS;
class A
{
CLASS_STUFF(A);
};
class B : public A
{
CLASS_STUFF(B);
};
class C
{
};
#include <iostream>
int main()
{
std::cout << IsPolymorphic<A>::value << std::endl;
std::cout << IsPolymorphic<B>::value << std::endl;
std::cout << IsPolymorphic<C>::value << std::endl;
std::cout << IsPolymorphic<int>::value << std::endl;
StartOfObject<A>::getStart(new A());
StartOfObject<B>::getStart(new B());
StartOfObject<C>::getStart(new C());
StartOfObject<int>::getStart(new int());
return 0;
};